在我的Spring Boot应用程序中,我有以下@RestController
方法:
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/{decisionId}/decisions/{childDecisionId}/characteristics/{characteristicId}/values", method = RequestMethod.POST)
public ValueResponse create(@PathVariable @NotNull @DecimalMin("0") Long decisionId, @PathVariable @NotNull @DecimalMin("0") Long childDecisionId, @PathVariable @NotNull @DecimalMin("0") Long characteristicId,
@Valid @RequestBody CreateValueRequest request, Authentication authentication) {
....
request.getValue()
...
}
这是我的CreateValueRequest
DTO:
public class CreateValueRequest implements Serializable {
private static final long serialVersionUID = -1741284079320130378L;
@NotNull
private Object value;
...
}
值可以是例如String
,Integer
,Double
以及相应的数组,例如String[]
,Integer[]
..等等
如果String
,Integer
,Double
一切正常,我的控制器方法中输入的类型正确。但是当我在我的控制器方法中发送一个数组时,我得到List
而不是数组。
是否有可能(如果是这样 - 如何)配置Spring + Jackson以获取数组(仅在此特定情况下)而不是List
request.getValue()
答案 0 :(得分:3)
执行该操作的杰克逊配置为USE_JAVA_ARRAY_FOR_JSON_ARRAY
,您可以阅读here。它会为您要反序列化的POJO中的Object[]
字段创建List
而不是Object
。使用此配置的示例:
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY);
Spring Boot here的文档介绍了如何配置Spring Boot使用的ObjectMapper
。基本上,您必须在相关的属性文件中设置此环境属性:
spring.jackson.deserialization.use_java_array_for_json_array=true