[英]Could not extract response: no suitable HttpMessageConverter with jaxb2marshaller
I setup on both client and server to have MarshallingMessageConverter using Jaxb2 我在客户端和服务器上都设置了使用Jaxb2的MarshallingMessageConverter
Follow from this question. 遵循这个问题。
This is on server-side: 这是在服务器端:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(marshallingMessageConverter());
}
public MarshallingHttpMessageConverter marshallingMessageConverter() {
return new MarshallingHttpMessageConverter(jaxb2Marshaller(), jaxb2Marshaller());
}
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[]{
User.class
});
return marshaller;
}
}
Client-side: 客户端:
@Configuration
public class RESTConfig {
@Bean
public MarshallingHttpMessageConverter marshallingMessageConverter() {
return new MarshallingHttpMessageConverter(jaxb2Marshaller(), jaxb2Marshaller());
}
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(new Class[] {
User.class
});
return marshaller;
}
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
//converters.addAll(restTemplate.getMessageConverters());
converters.add(marshallingMessageConverter());
restTemplate.setMessageConverters(converters);
return restTemplate;
}
}
Check on both log got: 检查两个日志得到:
org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbContextFromClasses
INFO: Creating JAXBContext with classes to be bound [class com.cloudlb.model.User]
when I try to retrieve User. 当我尝试检索用户时。 I got this
我懂了
org.springframework.web.client.RestClientException:
Could not extract response: no suitable HttpMessageConverter found for response type [com.cloudlb.model.User] and content type [application/xml]
Did I missing something? 我错过了什么吗?
I check by retrieve it as String.class and it work fine. 我通过将其检索为String.class进行检查,并且工作正常。 Got return in XML file format.
以XML文件格式返回。
I want to achieve the XML <--HTTP--> XML <-> Object> 我想实现XML <-HTTP-> XML <-> Object>
User user = restTemplate.getForObject(url, User.class);
Edited: This is my Controller 编辑:这是我的控制器
@Controller
@RequestMapping("/user")
public class UserServiceController {
@Autowired
private UserService userService;
public UserServiceController() {}
@RequestMapping(value="/{id}", method = RequestMethod.GET)
@ResponseBody
public User findUserById(@PathVariable("id") String id) {
return userService.findById(id);
}
}
Thank you in advance. 先感谢您。
When I configured this I defined a ContentNegotiatingViewResolver as follows: 当我配置它时,我定义了一个ContentNegotiatingViewResolver,如下所示:
@Bean
public ContentNegotiatingViewResolver contentNegotiatingViewResolver() {
ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
Map<String, String> mediaTypes = new HashMap<String, String>();
mediaTypes.put("xml", "application/xml");
resolver.setMediaTypes(mediaTypes);
List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
viewResolvers.add(new BeanNameViewResolver());
resolver.setViewResolvers(viewResolvers);
return resolver;
}
and then from my Controller I returned: 然后从我的控制器返回:
return new ModelAndView("spotView", "spot", spot);
and then in my config I defined a bean with the same name as the view returned from the controller (because I'm using a BeanName resolver): 然后在我的配置中,我定义了一个与控制器返回的视图同名的bean(因为我使用的是BeanName解析器):
@Bean
public MarshallingView spotView() {
return new MarshallingView(jaxbMarshaller());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.