简体   繁体   English

找不到适用于响应类型[..]和内容类型[application / json]的HttpMessageConverter

[英]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.

相关问题 找不到适合响应类型和内容类型的HttpMessageConverter - no suitable HttpMessageConverter found for response type and content type 没有找到适合响应类型和内容类型的 HttpMessageConverter [application/json;charset=UTF-8] 发生异常 - No suitable HttpMessageConverter found for response type and content type [application/json;charset=UTF-8] exception occurs 没有找到适合响应类型的 HttpMessageConverter - no suitable HttpMessageConverter found for response type 没有为响应类型找到合适的HttpMessageConverter - Custom RestTemplate - No suitable HttpMessageConverter found for response type - Custom RestTemplate 找不到适合响应类型 [class org.springframework.http.ResponseEntity] 和内容类型 [application/octet-stream] 的 HttpMessageConverter - No suitable HttpMessageConverter found for response type [class org.springframework.http.ResponseEntity] and content type [application/octet-stream] 无法提取响应:没有找到适合响应类型 class 和内容类型 [text/html] 的 HttpMessageConverter - Could not extract response: no suitable HttpMessageConverter found for response type class and content type [text/html] 无法写入请求:找不到适合请求类型和内容类型的HttpMessageConverter [application / x-java-serialized-object] - Could not write request: no suitable HttpMessageConverter found for request type and content type [application/x-java-serialized-object] Android版Spring-无法提取响应:找不到适合响应类型的HttpMessageConverter - Spring for Android - Could not extract response: no suitable HttpMessageConverter found for response type RestClientException无法提取响应:找不到合适的HttpMessageConverter作为响应类型 - RestClientException Could not extract response: no suitable HttpMessageConverter found for response type 转换JSON响应时找不到合适的HttpMessageConverter? - No suitable HttpMessageConverter found when converting JSON response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM