當我使用String restTempate檢索JSON時出現錯誤。錯誤說在Spring restTemplate中找不到HttpMessageConverter
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
傑克遜消息變換。
的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>
這裏是我的代碼使用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
}
}
}
奇怪的是,沒有任何錯誤,當我呼籲findPhoneByImei
方法findDeviceHoldersByUserId
方法,但誤差。這一直在努力。
我一次又一次嘗試,但我仍然有錯誤。請幫助解決問題。
謝謝。原因
我沒有看到你在哪裏聲明瞭名稱或id爲jacksonMessageConverter的bean - 你能否澄清這個bean是在哪裏定義的 - 我很抱歉如果我錯過了這個聲明。 –
@Dave我已經把它,但由於格式問題,它沒有顯示問題中的屬性。我修好了它。 – sunghun
謝謝編輯回來 - 下一個問題 - 傑克遜你正在使用 - 1或2 - 我在印象之下1.試試這個 - 你可以在調試器中運行這段代碼嗎?在HttpMessageConverterExtractor.extractData()中放置一個斷點並確認它確實已經註冊了轉換器,以及被檢測轉換器的'canRead'方法可以解釋該響應。 –