2

I setup on both client and server to have MarshallingMessageConverter using Jaxb2

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. Got return in XML file format.

I want to achieve the 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.

Community
  • 1
  • 1
xyzxyz442
  • 173
  • 3
  • 4
  • 11

1 Answers1

1

When I configured this I defined a ContentNegotiatingViewResolver as follows:

@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
public MarshallingView spotView() {
    return new MarshallingView(jaxbMarshaller());
}
Alex Barnes
  • 7,174
  • 1
  • 30
  • 50
  • What if my controller is return as Object(User)? Is this can be achieve? – xyzxyz442 Jan 05 '12 at 21:11
  • You can just return a ModelAndView passing in your User. Then define a bean called userView as in my example. – Alex Barnes Jan 05 '12 at 21:25
  • 1
    Its got 406 HTTP ERROR. I figure it out that I forget to put `@XmlRootElement` as @Grzegorz Grzybez mention in User domain object in client side. I put only on server-side – xyzxyz442 Jan 06 '12 at 07:32