[英]No suitable HttpMessageConverter found for response type [..] and content type [application/json]
I use Spring for Android to parse my Json. 我使用Spring for Android解析我的Json。
But I recently have this error : 但是我最近有这个错误:
No suitable HttpMessageConverter found for response type [...request.Temp] and content type [application/json]
找不到适用于响应类型[... request.Temp]和内容类型[application / json]的HttpMessageConverter
Even if I use MappingJackson2HttpMessageConverter which is able to handle application/json 即使我使用能够处理application / json的MappingJackson2HttpMessageConverter
Here is my request code : 这是我的请求代码:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.setAcceptEncoding(ContentCodingType.GZIP);
MultiValueMap<String, String> map=getParameters();
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(map, headers);
RestTemplate template=getRestTemplate();
template.setErrorHandler(new MyDefaultReponseErrorHandler());
/* I also tried it
MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter();
List<MediaType> mediaTypeList=new ArrayList<>();
mediaTypeList.add(new MediaType("application", "json"));
converter.setSupportedMediaTypes(mediaTypeList);
*/
List<HttpMessageConverter<?>> converterList=new ArrayList<>();
converterList.add(new FormHttpMessageConverter());
//converterList.add(converter);
converterList.add(new MappingJackson2HttpMessageConverter());
template.setMessageConverters(converterList);
return template.postForObject(endPoint, entity, clazz);
And the object that I try to parse : 和我尝试解析的对象:
{
"error": null,
"result": [
{ .. some fields .. }
]
}
With : 与:
@JsonIgnoreProperties(ignoreUnknown=true)
@JsonAutoDetect(fieldVisibility= JsonAutoDetect.Visibility.NONE)
public class Temp {
private int error;
private ArrayList<Search> arrayList;
public int getError() {
return error;
}
@JsonProperty("error")
public void setError(int error) {
this.error = error;
}
public ArrayList<Search> getArrayList() {
return arrayList;
}
@JsonProperty("result")
public void setArrayList(ArrayList<Search> arrayList) {
this.arrayList = arrayList;
}
}
With other functions (without array) of WebService this work. 与WebService的其他功能(无数组)一起工作。
Another thing that I find strange, when I parse the result with : 当我解析结果时,我发现另一件事很奇怪:
@JsonProperty("result")
public void setResult(String string) {
}
I have error of parsing. 我有解析错误。 It's normal, string different of array, but at least he try to parse.
这是正常的,字符串与数组不同,但是至少他尝试解析。
So, an idea ? 那么,一个主意?
PS : I try on another function with array to parse, it works PS:我尝试使用另一个函数来解析数组,它可以工作
Ok I found the bug, it come from my object Search (for my array of Search) : 好的,我发现了这个错误,它来自我的对象Search(对于我的Search数组):
@JsonProperty("start_date")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
@JsonProperty("start_date")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
I have used two times "start_date", the error message was not really explicit... 我已经使用了两次“ start_date”,错误消息不是很明显。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.