Reputation: 2746
I've been working on a a controller in which I want to return json string as a response. But the problem is that, I want to change some field names during serialize/deserialize, but I don't want to use ugly annotations on my entity objects.
Lets say
@Controller
@RequestMapping("/kfc/brands")
public class JSONController {
@RequestMapping(value="{name}", method = RequestMethod.GET)
public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
Shop shop = new Shop();
shop.setName(name);
shop.setStaffNames(new String[]{"mkyong1", "mkyong2"});
return shop;
}
}
public class Shop {
String name;
String staffNames[];
String location;
//getter and setter methods
}
I want the controller to return staffNames as staff_names
, location as address
without using any annotations.
I assume there has to be a custom object mapper structure to to that but couldn't find a proper example. I have no problems with setting field names manually in the serialization code.
PS: example taken from mkyong
Upvotes: 1
Views: 1124
Reputation: 2565
To enable the transformation from Camel cases fields Names like firstName
to underscore field name like field_name
you should register a custom json2Object Conveter I suppose that you have spring 3.1 or later
1. Configure you context
1-1. If you use XML Configuration then you put this code in you configuration file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- the important part start from here-->
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="objectMapper"
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="propertyNamingStrategy" >
<util:constant static-field="com.fasterxml.jackson.databind.PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES"/>
<property name="indentOutput" value="true"/>
</property>
</bean>
1-2. If you use programmatic configuartion
@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.indentOutput(true)
.propertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
}
}
2.Fetching dependencies
Download these jars and put them in your application jackson-annotations-2.7.2
,jackson-core-2.7.2
,jackson-databind-2.7.2
Here is the Maven repository
this converter will convert all REST messages with this header Content-Type=application/json
PS: this converter will not convert your json message to string since String doesn't have a default constructor , to read you JSON message as string in your controller you use Content-Type=applciation/text in your client message header
Upvotes: 1