使用 Java 对象作为请求参数分解为 swagger 中的原语 ui springfox
Using Java objects as request parameters breaks down to primitives in swagger ui springfox
我有以下控制器代码
@GetMapping("/users")
public ResponseEntity<UserDto> getUsers(Filter filter) {
return ResponseEntity.ok(userService.findUsers(filter));
}
Filter.java:
public class Filter {
private Integer page;
private Integer size;
// Contains 2 fields: "propertyName" and "order"
private Sort sort;
... getters and setters
}
URL 如下:/users?page=1&size=10&sort=+firstName
。所以我有一个从 String
到 Sort
的自定义转换器,它工作得很好。
但是,生成的 swagger 文档不正确:
"parameters":[
{
"name":"sort.propertyName",
"in":"query",
"required":false,
"type":"string"
},
{
"name":"sort.order",
"in":"query",
"required":false,
"type":"string",
"enum":[
"+",
"-"
]
},
{
"name":"page",
"in":"query",
"required":false,
"type":"integer",
"format":"int32"
},
{
"name":"size",
"in":"query",
"required":false,
"type":"integer",
"format":"int32"
}
]
如你所见,它分解了Filter
的Sort
字段,并生成了2个参数:sort.propertyName
和sort.order
。相反,我想要一个类型为 string
.
的参数 sort
有什么办法可以实现吗?我尝试用 @ApiParam(name = "sort", value = "Sort", type = "string")
注释 sort
字段,但它不起作用。
您可以创建一个备用类型规则,将 Sort
视为 String
import static springfox.documentation.schema.AlternateTypeRules.*;
...
// Configure your docket bean
new Docket(...)
...
.alternateTypeRules(newRule(Sort.class, String.class)
...
我有以下控制器代码
@GetMapping("/users")
public ResponseEntity<UserDto> getUsers(Filter filter) {
return ResponseEntity.ok(userService.findUsers(filter));
}
Filter.java:
public class Filter {
private Integer page;
private Integer size;
// Contains 2 fields: "propertyName" and "order"
private Sort sort;
... getters and setters
}
URL 如下:/users?page=1&size=10&sort=+firstName
。所以我有一个从 String
到 Sort
的自定义转换器,它工作得很好。
但是,生成的 swagger 文档不正确:
"parameters":[
{
"name":"sort.propertyName",
"in":"query",
"required":false,
"type":"string"
},
{
"name":"sort.order",
"in":"query",
"required":false,
"type":"string",
"enum":[
"+",
"-"
]
},
{
"name":"page",
"in":"query",
"required":false,
"type":"integer",
"format":"int32"
},
{
"name":"size",
"in":"query",
"required":false,
"type":"integer",
"format":"int32"
}
]
如你所见,它分解了Filter
的Sort
字段,并生成了2个参数:sort.propertyName
和sort.order
。相反,我想要一个类型为 string
.
sort
有什么办法可以实现吗?我尝试用 @ApiParam(name = "sort", value = "Sort", type = "string")
注释 sort
字段,但它不起作用。
您可以创建一个备用类型规则,将 Sort
视为 String
import static springfox.documentation.schema.AlternateTypeRules.*;
...
// Configure your docket bean
new Docket(...)
...
.alternateTypeRules(newRule(Sort.class, String.class)
...