我正在尝试读取json请求然后创建响应。请求属于“Request.java”,Response属于“WsResponse.java”。 WsResponse.java链接到contacts.java。当使用邮递员时,实现了适当的响应。但是当试图通过客户端调用调用方法时,它会给出异常
"Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@44c8afef; line: 1, column: 14] (through reference chain: com.websystique.springmvc.model.Request["contacts"]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@44c8afef; line: 1, column: 14] (through reference chain: com.websystique.springmvc.model.Request["contacts"]->java.util.ArrayList[0])
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:227)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:212)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:622)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580)
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:380)
at com.websystique.springmvc.Test.main(Test.java:49)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@44c8afef; line: 1, column: 14] (through reference chain: com.websystique.springmvc.model.Request["contacts"]->java.util.ArrayList[0])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:835)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseString(StdDeserializer.java:810)
at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:190)
at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:161)
at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:19)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:335)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:142)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3562)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2662)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:224)
... 6 more"
下面是课程:
public class Request {
@JsonProperty
private String blocking;
@JsonProperty
private String module;
@JsonProperty("contacts")
private List<String> contacts;
public Request(){
}
public String getBlocking() {
return blocking;
}
public void setBlocking(String blocking) {
this.blocking = blocking;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
public List<String> getContacts() {
return contacts;
}
public void setContacts(List<String> contacts) {
this.contacts = contacts;
}
@Override
public String toString() {
return "Request [blocking=" + blocking + ", module=" + module + ", contacts=" + contacts + "]";
}
}
public class WsResponse {
@JsonProperty("contacts")
private List<contacts> contacts;
public WsResponse() {
}
public List<contacts> getContacts() {
return contacts;
}
public void setContacts(List<contacts> contacts) {
this.contacts = contacts;
}
@Override
public String toString() {
return "[contacts:" + contacts + "]";
}
public class contacts {
private String input;
private String status;
private String wa_id;
public contacts() {
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getWa_id() {
return wa_id;
}
public void setWa_id(String wa_id) {
this.wa_id = wa_id;
}
@Override
public String toString() {
return "{input=" + input + ", status=" + status + ", wa_id=" + wa_id + "}";
}
}
public class Test {
public static void main(String[] args) throws JSONException {
final String uriForPost = "http://localhost:8080/Spring4MVCCRUDRestService/getContactDetails/";
ArrayList<String> contactList = new ArrayList<String>();
contactList.add("9315551000");
contactList.add("9315551002");
contactList.add("931-555-1005");
contactList.add("031-555-1005");
Request req = new Request();
req.setBlocking("wait");
req.setModule("Model_A");
req.setContacts(contactList);
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForObject(uriForPost, req, Request.class);
}
}
这里是我的请求JSON:
{
"blocking": "wait",
"module":"Model_A",
"contacts": [
"7855222121",
"988542d1222",
"9029674116",
"5252525252",
"213123123123"
]
}
回应(从Postman调用后):
{
"contacts": [
{
"input": "7855222121",
"status": "valid",
"wa_id": "7855222121"
},
{
"input": "988542d1222",
"status": "valid",
"wa_id": "988542d1222"
},
{
"input": "9029674116",
"status": "valid",
"wa_id": "9029674116"
},
{
"input": "5252525252",
"status": "valid",
"wa_id": "5252525252"
},
{
"input": "213123123123",
"status": "valid",
"wa_id": "213123123123"
}
]
}
尝试使用Stackoverflow和其他可用站点的解决方案:
1. using @JsonProperty on fields
2. Passing JSON object instead of Request class object
JSONObject json = new JSONObject();
json.put("blocking", "wait");
json.put("module", "Model_A");
ArrayList<String> contactList = new ArrayList<String>();
contactList.add("6315551000");
contactList.add("6315551002");
contactList.add("631-555-1005");
json.put("contacts", contactList);
3.Adding below code for convertor
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
//messageConverters.add(new MappingJacksonHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
4.
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM));
restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
任何提示或线索都会有所帮助。
谢谢
答案 0 :(得分:0)
您的代码中存在拼写错误(我认为),您期望以Request.class
作为回复,而您必须将其映射到WsResponse.class
// wrong mapping of response to Request.class
restTemplate.postForObject(uriForPost, req, Request.class);
// correct mapping
WsResponse res = restTemplate.postForObject(uriForPost, req, WsResponse.class);
由于在请求和响应中存在联系人字段而产生异常,但在请求中它是字符串数组并且作为响应它是对象(联系类)的数组,所以当jackson尝试时要将响应json deserilize为 Request#contacts ,它会找到一个起始对象标记 {,但它期待一个字符串。