Rectangle 27 1

json Spring MVC mapping view for Google GSON?


@Controller
public class AppController {
  @RequestMapping(value = "messages", produces = MediaType.APPLICATION_JSON_VALUE)
  public List<Message> getMessages() {
    // .. Get list of messages
    return messages;
  }
}
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(new GsonHttpMessageConverter());
  }
}

Register your GsonHttpMessageConverter with the message converters in your MVC config.

Thank you, this really helped me! With Spring 3.2.2 and jackson I could not get rid of the problem where it says: 406 not accaptable. I did this and it worked, but I also had to add <context:annotation-config/> to the service xml file.

The Spring docs outline this process, but aren't crystal clear. In order to get this to work properly, I had to extend WebMvcConfigurerAdapter, and then override configureMesageConverters. After doing this, you should be able to do the following in your controller method:

aweigold got me most of the way there, but to concretely outline a solution for Spring 3.1 Java based configuration, here's what I did.

Note