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

Java 在Spring MVC上用不同的字段名序列化JSON,java,json,spring-mvc,serialization,Java,Json,Spring Mvc,Serialization,我一直在开发一个控制器,希望在其中返回json字符串作为响应。但问题是,我想在序列化/反序列化期间更改一些字段名,但我不想在实体对象上使用难看的注释 比方说 @Controller @RequestMapping("/kfc/brands") public class JSONController { @RequestMapping(value="{name}", method = RequestMethod.GET) public @ResponseBody Shop get

我一直在开发一个控制器,希望在其中返回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

    }
我希望控制器返回
员工姓名作为员工姓名
位置作为地址
,而不使用任何注释

我假设必须有一个自定义的对象映射器结构来实现这一点,但找不到合适的示例。在序列化代码中手动设置字段名没有问题


PS:example from

要启用从驼峰大小写字段名(如
firstName
)到下划线字段名(如
field\u name
)的转换,您应该注册一个自定义json2Object Converter。我假设您使用的是spring 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()));
    }

}
@配置
@EnableWebMvc
公共类网络配置扩展了WebMVCConfigureAdapter{
@凌驾
public void configureMessageConverters(列表