简体   繁体   English

使用spring RESTtemplate检索Jsonobjects列表时出现异常

[英]i'm getting an exception when retrieving a list of Jsonobjects using springs RESTtemplate

I am trying to get a list of Json objects from a REST webservice. 我正在尝试从REST Web服务获取Json对象的列表。 I'm able to do the actual get request but when Spring tries to deserialize the object I get the following exception: 我能够执行实际的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

Here is an example of a list of Json objects that I want to retrieve: 这是我要检索的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"
  }]
}

heres the POJO I use to turn the jsonObjects into Java objects. 这是我用来将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 + '\'' +
                '}';
    }
}

and heres the service class I've made that uses springs REST template to make the GET request. 这里是我使用Springs 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){}



}

(the parameter accountId is being ignored on purpose, I've hardcoded the accountID in the url for testing purposes). (故意忽略了参数accountId,出于测试目的,我已在URL中对accountID进行了硬编码)。

Can anyone see what i'm doing wrong? 谁能看到我在做什么错?

Thank you. 谢谢。

In fact your JSON is not a list but an object with a records field. 实际上,您的JSON不是列表,而是带有records字段的对象。

Create a wrapper class 创建包装器类

public class TriggerRecordsWrapper {
    List<Trigger> records;
}

to parse the JSON or adapt the JSON to represent a list. 解析JSON或改编JSON以表示列表。

add implements Serializable to your trigger object. 将实现可序列化的工具添加到您的触发器对象。 and add this code to your appConfig File: 并将以下代码添加到您的appConfig文件中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用RestTemplate时出现消息“不支持HTTP协议”的异常 - Getting Exception with message “http protocol is not supported” when using RestTemplate 使用 Spring RestTemplate 请求 XSRF-TOKEN 时出现异常 - Getting exception when requesting XSRF-TOKEN using Spring RestTemplate 尝试通过Springs RestTemplate执行POST请求时获取RestClientException - Getting a RestClientException while trying to perform a POST request with Springs RestTemplate 使用Spring时获取HttpMessageNotReadableException异常-RestTemplate - Get HttpMessageNotReadableException Exception when using Spring - RestTemplate 当我继承OnClickListener()的类时,出现空指针异常 - I'm getting a null pointer exception when I subclass an OnClickListener() 使用休眠获取数据时获取异常 - Getting exception in retrieving data using hibernate 不能为 child() 中的参数“pathString”传递 null? 这是我从 firebase 检索数据时遇到的错误 - Can't pass null for argument 'pathString' in child()? This the error I'm getting when retrieving data from firebase REST POST 与 POSTMAN 一起正常工作,但在使用 Spring RestTemplate 时出现异常 - REST POST works correctly with POSTMAN but exception when using Spring RestTemplate 我正在使用 SpringBoot 和 Jquery 和 Ajax 编写一个 crud 应用程序。 我得到以下异常 - I'm writing a crud application using SpringBoot and Jquery with Ajax. And I'm getting the following exception 使用 spring restTemplate 发送 post 请求时出现异常 - have an exception when using spring restTemplate to send a post request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM