Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring configure @ResponseBody JSON format

Imagine I have this annotated method in a Spring 3 @Controller

@RequestMapping("") public @ResponseBody MyObject index(@RequestBody OtherObject obj) {     MyObject result = ...;     return result; } 

But I need to configure the output json format, just as if I were doing:

ObjectMapper om = new ObjectMapper(); om.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, true); om.getSerializationConfig()         .setSerializationInclusion(JsonSerialize.Inclusion.NON_DEFAULT); om.getSerializationConfig()         .set(SerializationConfig.Feature.INDENT_OUTPUT, false); 

Is there any way to configure this behaviour?

I've found a couple of related questions, but I am not sure about how to adapt them to my specific case:

  1. spring prefixjson with responsebody
  2. Who sets response content-type in Spring MVC (@ResponseBody)

Thank you !

like image 322
Guido Avatar asked Jan 27 '11 23:01

Guido


People also ask

What is @ResponseBody annotation in Spring?

The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object. When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the HTTP response automatically.

How do I change the response type dynamically in Spring rest?

This is done by setting the Accept header to the required type(s). So in the case where the consumer expects a response type as JSON an Accept header should be configured with "application/json" or "application/xml" for XML (or any specific variation of each such as "application/xhtml+xml" and alike).


2 Answers

For the folks who are using Java based Spring configuration:

@Configuration @ComponentScan(basePackages = "com.domain.sample") @EnableWebMvc public class SpringConfig extends WebMvcConfigurerAdapter { ....      @Override     public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {         final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();         final ObjectMapper objectMapper = new ObjectMapper();         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);         converter.setObjectMapper(objectMapper);         converters.add(converter);         super.configureMessageConverters(converters);     }  ....  } 

I'm using MappingJackson2HttpMessageConverter - which is from fasterxml.

<dependency>   <groupId>com.fasterxml.jackson.core</groupId>   <artifactId>jackson-databind</artifactId>   <version>2.3.1</version> </dependency> 

If you want to use codehaus-jackson mapper, instead use this one MappingJacksonHttpMessageConverter

 <dependency>     <groupId>org.codehaus.jackson</groupId>     <artifactId>jackson-mapper-asl</artifactId>     <version>${codehaus.jackson.version}</version>  </dependency> 
like image 193
Jama A. Avatar answered Sep 27 '22 18:09

Jama A.


I needeed to solve very similar problem, which is configuring Jackson Mapper to "Do not serialize null values for Christ's sake!!!".

I didn't want to leave fancy mvc:annotation-driven tag, so I found, how to configure Jackson's ObjectMapper without removing mvc:annotation-driven and adding not really fancy ContentNegotiatingViewResolver.

The beautiful thing is that you don't have to write any Java code yourself!

And here is the XML configuration (don't be confused with different namespaces of Jackson classes, I simply used new Jakson 2.x library ... the same should also work with Jackson 1.x libraries):

<mvc:annotation-driven>     <mvc:message-converters register-defaults="true">         <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">             <property name="objectMapper">                 <bean class="com.fasterxml.jackson.databind.ObjectMapper">                     <property name="serializationInclusion">                         <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>                     </property>                 </bean>             </property>         </bean>     </mvc:message-converters> </mvc:annotation-driven> 
like image 20
tkuty Avatar answered Sep 27 '22 19:09

tkuty