0

使用 String restTempate 检索 JSON 时出现错误。错误说

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)

我发现该消息意味着没有注册 JSON 转换器,但我的xml.

应用程序上下文.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>

这是我使用 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
}

响应包如下:

{
   "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
       }
   }
}

findDeviceHoldersByUserId奇怪的是,我调用方法时没有错误,但方法错误findPhoneByImei。这一直持续到之前。

我一次又一次地尝试,但我仍然有错误。请帮助解决问题。

谢谢。

4

1 回答 1

1

我发现可能导致此问题的几个原因是 -

  1. setter/getter 的数据类型与实际属性的数据类型不同(sunghun 的评论中也提到过)
  2. 如果有可能看起来像字段的 setter/getter 的重载方法 - 与 setField 或 getField 同名,其中字段是类的属性。

我有一个字段private boolean success和 2 个 setter 方法 -

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

在调试时,我发现该类com.fasterxml.jackson.databind.deser.BeanDeserializerFactory正在引发异常。此异常在 Jackson 的库中被抑制,并且 RestTemplate 抛出的异常与主题相同。

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

我将方法更改为public void setSuccessData(List<Object> dataList);并且效果很好。

希望这可以帮助某人。

于 2013-10-22T04:38:19.697 回答