Java spring boot将单个对象作为数组
我在将单个对象解析为数组时遇到问题。我使用了以下属性Java spring boot将单个对象作为数组,java,spring-boot,Java,Spring Boot,我在将单个对象解析为数组时遇到问题。我使用了以下属性 # JACKSON (JacksonProperties). spring.jackson.deserialization.ACCEPT_SINGLE_VALUE_AS_ARRAY=true 除上述内容外,我还配置了一个rest模板,如下所示: @Test public void testSingleObject() { RestTemplate restTemplate = new RestTemplate();
# JACKSON (JacksonProperties).
spring.jackson.deserialization.ACCEPT_SINGLE_VALUE_AS_ARRAY=true
除上述内容外,我还配置了一个rest模板,如下所示:
@Test
public void testSingleObject() {
RestTemplate restTemplate = new RestTemplate();
ObjectMapper mapper = new ObjectMapper();
MappingJackson2HttpMessageConverter convertor = new MappingJackson2HttpMessageConverter();
convertor.setObjectMapper(mapper);
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
restTemplate.getMessageConverters().add(convertor);
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
mockServer.expect(requestTo("/test/1")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("{\"size\":\"1\",\"Value\":{\"@id\": \"1\",\"description\":\"Some Text\"}}", MediaType.APPLICATION_JSON));
// JsonNode jsonNode = restTemplate.getForObject("/test/{id}", JsonNode.class, 1);
mycalss value = restTemplate.getForObject("/test/{id}", myclass.class, 1);
System.out.print(value.toString());
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"description"
})
public class mysubclass {
@JsonProperty("@id")
private String Id;
@JsonProperty("description")
private String description;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
我使用的是spring boot 1.3.2,得到的错误是
Could not read document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
Ps:如果我在没有rest模板的情况下尝试这个,它会按预期工作
@Test
public void testSingleObject3() throws IOException {
final String json = "{\"size\":\"1\",\"Value\":{\"@id\": \"1\",\"description\":\"Some Text\"}}";
final ObjectMapper mapper = new ObjectMapper()
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
myclass value = mapper.readValue(json,
new TypeReference<myclass>() {
});
System.out.println(value.toString());
}
@测试
public void testSingleObject3()引发IOException{
最后一个字符串json=“{\'size\':\'1\',\'Value\':{\'@id\':\'1\',\'description\':\'Some Text\'}”;
最终对象映射器映射器=新对象映射器()
.enable(反序列化功能。接受\u单个\u值\u作为\u数组);
myclass value=mapper.readValue(json,
新类型引用(){
});
System.out.println(value.toString());
}
我的类定义如下:
//@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class myclass {
@JsonProperty("size")
private String Size;
@JsonProperty("value")
private List<mysubclass> mysubclass;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
//... setters and getters
/@JsonFormat(with=JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
@JsonInclude(JsonInclude.Include.NON_NULL)
公共类myclass{
@JsonProperty(“大小”)
私有字符串大小;
@JsonProperty(“值”)
私有列表mysubclass;
@杰索尼奥雷
私有映射additionalProperties=new HashMap();
//…二传手和接球手
mysubclass的定义如下:
@Test
public void testSingleObject() {
RestTemplate restTemplate = new RestTemplate();
ObjectMapper mapper = new ObjectMapper();
MappingJackson2HttpMessageConverter convertor = new MappingJackson2HttpMessageConverter();
convertor.setObjectMapper(mapper);
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
restTemplate.getMessageConverters().add(convertor);
MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
mockServer.expect(requestTo("/test/1")).andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("{\"size\":\"1\",\"Value\":{\"@id\": \"1\",\"description\":\"Some Text\"}}", MediaType.APPLICATION_JSON));
// JsonNode jsonNode = restTemplate.getForObject("/test/{id}", JsonNode.class, 1);
mycalss value = restTemplate.getForObject("/test/{id}", myclass.class, 1);
System.out.print(value.toString());
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"description"
})
public class mysubclass {
@JsonProperty("@id")
private String Id;
@JsonProperty("description")
private String description;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
“身份证”,
“说明”
})
公共类mysubclass{
@JsonProperty(“@id”)
私有字符串Id;
@JsonProperty(“描述”)
私有字符串描述;
@杰索尼奥雷
私有映射additionalProperties=new HashMap();
当你这样做时,问候
restTemplate.getMessageConverters().add(converter);
实际上,您正在将第二个jackson转换器添加到RestTemplate
的转换器列表中
运行代码时,将使用第一个(默认情况下由restemplate
构造函数添加),因此配置第二个并不重要
如果你把那段代码改成
restTemplate.setMessageConverters(Collections.singletonList(converter));
默认转换器将被丢弃,只使用您配置的转换器。您还可以提供
myclass
的代码吗?您的问题帮助回答了我自己的问题。谢谢。