2012-05-10 70 views
1

我試圖通過restTemplate連接到服務器端以檢索xml。但我正在接受一個RestClientException和這條消息:「無法提取響應:沒有找到合適的HttpMessageConverter響應類型[frontend.model.Registration]和內容類型[application/xml]」 在調度程序servlet我寫這個:RestClientException無法提取響應彈簧mvc

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
<property name="mediaTypes"> 
    <map> 
     <entry key="xml" value="application/xml"/> 
     <entry key="atom" value="application/atom+xml"/> 
     <entry key="html" value="text/html"/> 
    </map> 
</property> 
<property name="viewResolvers"> 
    <list> 
     <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> 
    </list> 
</property> 

事後我補充一點:

<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/> 

而且異常出現在這一行:3

ResponseEntity<Registration> result = restTemplate.exchange("http://www.../ckp/user/{id}", 
        HttpMethod.GET, entity, Registration.class, id); 

我現在不能解決問題..我想通過我不知道哪些解析器和哪些轉換器來添加ViewResoler和MessageConverter。任何人都可以提出一些建議嗎? 我應該在disptcher servlet上添加一些東西嗎?我應該添加一個庫嗎? 我的模型類是pojo包含jaxb註釋。

回答

2

您需要將xml消息轉換器bean添加到RestTemplate bean定義中。這是我用:

​​

不要忘了(通過XML或註解)注入restTemplate豆到類。

編輯:在您的課堂,你叫RestTemplate,添加一個字段是這樣的:

@Inject 
private RestTemplate restTemplate; 
+0

謝謝你的迴應你的意思是注射類是什麼,對不起,我新手!?。此外,我需要添加一個新的庫來支持這個? –

+0

你能給我一個例子嗎?還有一個問題,只有:我需要爲application/xml.Isn't? –

+0

是的,如果您只想使用XML Web服務,則只需要該轉換器。請參閱答案中的編輯。你可能想使用@Autowired註解(javax.inject.Inject的spring相當於)。 – nickdos