JSON在Spring MVC上使用不同的字段名序序列化

时间:2016-03-03 11:25:07

标签: java json spring-mvc serialization

我一直在研究一个控制器,我想在其中返回json字符串作为响应。但问题是,我想在序列化/反序列化期间更改一些字段名称,但我不想在我的实体对象上使用丑陋的注释。

让我们说

@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

    }

我希望控制器在不使用任何注释的情况下返回staffNames as staff_nameslocation as address

我认为必须有一个自定义对象映射器结构,但无法找到一个正确的例子。我在序列化代码中手动设置字段名称没有问题。

PS:取自mkyong

的示例

1 个答案:

答案 0 :(得分:1)

要启用从Camel案例字段转换,像firstName这样的名称下划线字段名称,如field_name,您应该注册一个自定义的json2Object Conveter我想你有春季3.1或更高版本

  • 对于步骤1-1和1-2,您只执行其中一个而不是两个。

<强> 1。配置您的上下文

<强> 1-1。如果您使用XML配置,则将此代码放入配置文件

    <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。如果您使用程序化配置

 @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.获取依赖关系

下载这些广告并将其放入您的应用jackson-annotations-2.7.2jackson-core-2.7.2jackson-databind-2.7.2 Here is the Maven repository

此转换器将使用此标头Content-Type=application/json

转换所有REST消息
  

PS:此转换器不会将您的json消息转换为字符串,因为String没有默认构造函数,要在控制器中将JSON消息作为字符串读取您在客户端消息头中使用Content-Type = applciation / text < / p>