Announcement Announcement Module
Collapse
No announcement yet.
Add HttpMessageConverter as Supported Controller Method Argument? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Add HttpMessageConverter as Supported Controller Method Argument?

    Hi All,

    I love using <mvc:annotation-driven /> to setup a MappingJacksonHttpMessageConverter and being able to use the @ResponseBody annotation in my controllers to indicate the return value should be serialized. Awesome.

    However, this is somewhat limiting from a customization POV. I have a Jackson Mixins I would like to use in certain controller actions, but since I have no access to the underlying ObjectMapper, I have to do something like this.

    Code:
    @RequestMapping(value="/datasets/{id}", method = RequestMethod.GET)
    public void findDatasetById(@PathVariable Integer id, Writer writer) {
    	List<Dataset> datasets = datasetService.findDatasetById(id);
    
    		
    	ObjectMapper om = new ObjectMapper();
    	om.getSerializationConfig().addMixInAnnotations(Target.class, MixIn.class);
    
    	String json = om.writeValueAsString(datasets);
    
    	writer.write(json);
    }
    This isn't ideal from a testing POV, since the return type is void. (I'm creating my own ObjectMapper and directly writing to the response.)

    Is there any way to get access to the ObjectMapper in the HttpMessageConverter created by <mvc:annotation-driven />? And can it be made a prototype bean? That would be so useful!

    Thanks
    Last edited by atwoodjw; Mar 12th, 2012, 05:10 PM.
Working...
X