我在使用spring RESTtemplate检索Jsonobjects列表时遇到异常

时间:2017-08-25 09:01:09

标签: java spring rest spring-rest

我正在尝试从REST Web服务获取Json对象列表。我能够执行实际的get请求但是当Spring尝试反序列化对象时,我得到以下异常:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: java.io.PushbackInputStream@593f72fb; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: java.io.PushbackInputStream@593f72fb; line: 1, column: 1]
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:228) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:213) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:884) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:868) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:622) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:526) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        at nl.enshore.chatstreamer.service.SmartsuppRestServiceImpl.listTriggers(SmartsuppRestServiceImpl.java:36) ~[main/:na]
        at nl.enshore.chatstreamer.smack.TSmackMessageHandler.handleChatMessage(TSmackMessagr.java:81) ~[main/:na]
        at nl.enshore.chatmanager.listeners.HandledMessageListener.processMessage(HandledMessageListener.java:32) ~[enshore-chat-connection-1.1.2.jar:1.1.2]
        at org.jivesoftware.smack.chat.Chat.deliver(Chat.java:180) ~[smack-im-4.1.0.jar:4.1.0]
        at org.jivesoftware.smack.chat.ChatManager.deliverMessage(ChatManager.java:351) ~[smack-im-4.1.0.jar:4.1.0]
        at org.jivesoftware.smack.chat.ChatManager.access$300(ChatManager.java:53) ~[smack-im-4.1.0.jar:4.1.0]
        at org.jivesoftware.smack.chat.ChatManager$2.processPacket(ChatManager.java:162) ~[smack-im-4.1.0.jar:4.1.0]
        at org.jivesoftware.smack.AbstractXMPPConnection$4.run(AbstractXMPPConnection.java:1126) ~[smack-core-4.1.0.jar:4.1.0]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
 at [Source: java.io.PushbackInputStream@593f72fb; line: 1, column: 1]
        at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148) ~[jackson-databind-2.6.1.jar:2.6.1]
        at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:854) ~[jackson-databind-2.6.1.jar:2.6.1]
        at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:850) ~[jackson-databind-2.6.1.jar:2.6.1]
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:292) ~[jackson-databind-2.6.1.jar:2.6.1]
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:227) ~[jackson-databind-2.6.1.jar:2.6.1]
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217) ~[jackson-databind-2.6..6.1]:2
        at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25) ~[jackson-databind-2.6.1.jar:2.6.1]
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3702) ~[jackson-databind-2.6.1.jar:2.6.1]
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2798) ~[jackson-databind-2.6.1.jar:2.6.1]
        at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:225) ~[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE]
        ... 18 common frames omitted

以下是我想要检索的Json对象列表的示例:

{
  "records": [{
    "id": 563,
    "active": true,
    "name": "1st visit greeting",
    "description": "Greeting message to a visitor who visits your website for the 1st time"
  }, {
    "id": 564,
    "active": false,
    "name": "2st visit greeting",
    "description": "Greeting message to a visitor who visits your website for the 2nd time"
  }]
}

这是我用来将jsonObjects变成Java对象的POJO。

@JsonIgnoreProperties(ignoreUnknown = true)
public class Trigger {

    private long id;

    @JsonProperty("name")
    private String triggerName;

    private String description;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTriggerName() {
        return triggerName;
    }

    public void setTriggerName(String triggerName) {
        this.triggerName = triggerName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "Trigger{" +
                "id=" + id +
                ", triggerName='" + triggerName + '\'' +
                ", description='" + description + '\'' +
                '}';
    }
}

并且继承了我使用spring REST模板发出GET请求的服务类。

@Service
public class SmartsuppRestServiceImpl implements SmartsuppRestService {

    private final RestTemplate restTemplate;

    @Autowired
    public SmartsuppRestServiceImpl(RestTemplate restTemplate){
        this.restTemplate = restTemplate;
    }

    @Override
    public List<Trigger> listTriggers(String accountId){
        HttpHeaders apiKeyHeader = new HttpHeaders();
        apiKeyHeader.set("apiKey", "************");
        HttpEntity entity = new HttpEntity(apiKeyHeader);

        ResponseEntity<List<Trigger>> getResponse = restTemplate.exchange("https://api.smartsupp.com/accounts/****/triggers"
                , HttpMethod.GET, entity, new ParameterizedTypeReference<List<Trigger>>(){});
        return getResponse.getBody();
    }

   // public Trigger getTrigger(int triggerId){}



}

(有意忽略参数accountId,我已经在网址中硬编码了accountID以进行测试)。

谁能看到我做错了什么?

谢谢。

2 个答案:

答案 0 :(得分:2)

实际上,您的JSON不是列表,而是具有records字段的对象。

创建一个包装类

public class TriggerRecordsWrapper {
    List<Trigger> records;
}

解析JSON或调整JSON以表示列表。

答案 1 :(得分:0)

将实现Serializable添加到您的触发器对象。并将此代码添加到appConfig文件:

 @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper;
    }