REST WebサービスからJsonオブジェクトのリストを取得しようとしています。私は、実際のGET要求を行うことができるよしかし、春には、私は次の例外を取得するには、オブジェクトをデシリアライズしようとすると:springを使用してJsonobjectsのリストを取得するときに例外が発生するRESTtemplate
:ここorg.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: [email protected]; 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: [email protected]; 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: [email protected]; 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 + '\'' +
'}';
}
}
私がGETリクエストを作成するためにスプリングRESTテンプレートを使用して作成したサービスクラスをheresしました。
@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は意図的に無視されていますが、私はテスト目的でURLにaccountIDをハードコードしています)。
誰でも私が間違っているのを見ることができますか?
ありがとうございます。
ほんの少しサイドノート:私は直接 'Autowired' @メンバー' restTemplate'だろうと 'SmartsuppRestServiceImpl'を取り除きます –