在表示查询参数作为对象的情况下,杰克逊注释被忽略
因此有一个简单的 GET 端点能够接受一些 url-params。为了以更优雅的方式与它们一起工作,正在使用 POJO。此外,所有 POJO 字段都使用 swagger 注释进行注释。
@GetMapping
public Object getSomething(@Valid QueryParams queryParams,
HttpServletRequest request) {
// do smth with queryParams
return something
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ApiModel(value = "QueryParams", description = "some description")
public class QueryParams {
//some other fields
@ApiModelProperty(value = "Some description", example = "false")
@JsonDeserialize(using = StrictBooleanDeserializer.class)
@JsonProperty("is_archived")
private Boolean isArchived;
//some other fields
}
问题在于,没有 Jackson 注释实际应用于带注释的字段。不会调用 StrictBooleanDeserializer 内部的代码。
我知道有以下解决方法
@Autowired
private ObjectMapper objectMapper;
@GetMapping
public Object getSomething(Map<String, String> params,
HttpServletRequest request) {
QueryParams queryParams = objectMapper.convertValue(params, QueryParams.class)
// do smth with queryParams
return something
}
但是这样我们就失去了声明性验证和所有的招摇描述。 我认为必须有一种方法可以同时使用所有提到的功能,而无需太多开销。
So there is a simple GET endpoint which is able to accept some url-params. In order to work with them in more elegant way the POJO is being used. Also, all the POJO fields are annotated with swagger annotations
@GetMapping
public Object getSomething(@Valid QueryParams queryParams,
HttpServletRequest request) {
// do smth with queryParams
return something
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ApiModel(value = "QueryParams", description = "some description")
public class QueryParams {
//some other fields
@ApiModelProperty(value = "Some description", example = "false")
@JsonDeserialize(using = StrictBooleanDeserializer.class)
@JsonProperty("is_archived")
private Boolean isArchived;
//some other fields
}
The problem is that no Jackson annotation is actually applied to annotated fields. The code inside StrictBooleanDeserializer is not invoked.
I know there is the following workaround
@Autowired
private ObjectMapper objectMapper;
@GetMapping
public Object getSomething(Map<String, String> params,
HttpServletRequest request) {
QueryParams queryParams = objectMapper.convertValue(params, QueryParams.class)
// do smth with queryParams
return something
}
But in this way we lose the declarative validation and all the swagger descriptions.
I think there has to be a way to use all the mentioned features together without much of overhead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论