HttpMessageConverter not found in Spring restTempl

2020-05-06 11:25发布

问题:

I have got error when I retrieve JSON using String restTempate. Error says

Could not extract response: no suitable HttpMessageConverter found for response type  [class au.org.jeenee.mdm.models.PhoneResponse] and content type [application/json]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:107)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:492)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:447)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:295)
    at au.org.jeenee.mdm.services.EccClientServiceImpl.findPhoneByImei(EccClientServiceImpl.java:51)
    at au.org.jeenee.mdm.controllers.DeviceController.showEditForm(DeviceController.java:308)

I found out the message means there is no JSON converter registered but I have Jackson message converter in my xml.

applicationContext.xml

<bean id="jacksonMessageConverter"
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

<bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter" />
        </list>
    </property>
</bean>

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
  <constructor-arg>
      <bean class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
           <constructor-arg ref="httpClient"/>
       </bean>
   </constructor-arg>
   <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter" />
        </list>
    </property>
</bean>

And here is my code to use RestTemplate.

public class MyRestClientService {
@Override
    public List<DeviceHolder> findDeviceHoldersByUserId(String userId) {
        String uri = "/web/getpersonlist?userId=" + userId;
        try {
            DeviceHolderResponse response = restClient.getRestTemplate().postForObject(restClient.createUrl(uri), "", DeviceHolderResponse.class);


            if (response!=null && response.isOk() && response.getResult()!=null){

                DeviceHolder[] deviceHolders = response.getResult();
                return Arrays.asList(deviceHolders);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Collections.emptyList();
    }

    @Override
    public Phone findPhoneByImei(String imei) {
        log.info("findPhoneByImei:" + imei);

        Phone phone = null;
        String uri = "/ecc/getphoneplan?imei=" + imei;
        try {
            PhoneResponse response = restClient.getRestTemplate().postForObject(restClient.createUrl(uri), "", PhoneResponse.class);
            if (response.getResult()!=null)
                phone = response.getResult();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return phone;
    }
}

PhoneResponse.java
public class Phone implement Serializable {
   private boolean ok;
   private String message;
   private Phone result;

  //getters and setters
}


Phone.java
public class Phone implements Serializable {

    @JsonProperty(value="phoneid")
    private long phoneId;

    private Plan plan;
    private String sim;
    private String imei;

    @JsonProperty(value="phonetype")
    private String phoneType;

    @JsonProperty(value="phonenumber")
    private String phoneNumber;
    private String label;
    @JsonProperty(value="connecteddate")
    private String connectedDate;

//getters and setters
}

Plan.java
public class Plan implements Serializable {

    @JsonProperty(value="planid")
    private long planId;

    @JsonProperty(value="planname")
    private String planName;

    private double billingIncrement;

    private double owiStdUnitCost;

    private double owiFlagFall;

    private double stdCap;

    private double dataCap;

    private double smsCap;

    private double owiDataUnitCost;

    private double owiSms;

    //getters and setters
}

And the response packet is like following:

{
   "ok": true,
   "message": "",
   "result":
   {
       "phoneid": 600003,
       "phonenumber": 478439503,
       "phonetype": "Samsung Galaxy S2",
       "imei": "1111111111",
       "sim": "1111111111",
       "label": "Person name",
       "connecteddate": "2012-09-19 00:00:00.0",
       "plan":
       {
           "planid": 34,
           "planname": "$59 Plan",
           "billingIncrement": 30,
           "owiStdUnitCost": 81.8181818181818,
           "owiFlagFall": 0,
           "stdCap": 636.3636,
           "dataCap": 227.2665,
           "smsCap": 1363.638,
           "owiDataUnitCost": 0.022194,
           "owiSms": 22.7272727272727
       }
   }
}

Strangely, there is no error when I call findDeviceHoldersByUserId method but error for findPhoneByImei method. And this has been working up to just before.

I tried again and again but I still have the error. Please help to fix the problem.

Thanks.

回答1:

Couple of reasons, I found, that can cause this issue are -

  1. Data type of the setters/getters are different from that of the actual properties (also mentioned in comment of sunghun)
  2. If there are overloaded methods that may look like setter/getter of a field - same name as setField or getField where field is a property of the class.

I had a field private boolean success and 2 setter methods -

public void setSuccess(List<Object> dataList);
public void setSuccess(boolean success);

On debugging, I found that class com.fasterxml.jackson.databind.deser.BeanDeserializerFactory was throwing an exception. This exception was suppressed within Jackson's lib and the exception thrown by the RestTemplate was the same as the subject.

java.lang.IllegalArgumentException: Conflicting setter definitions for property "failure": com.test.dto.JsonResponse#setFailure(1 params) vs com.test.dto.JsonResponse#setFailure(1 params)

I changed the method to public void setSuccessData(List<Object> dataList); and it worked fine.

Hope this helps someone.



标签: spring rest