2012-05-10 71 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