【问题标题】:JSON serialize with different field names on Spring MVC在 Spring MVC 上使用不同的字段名称序列化 JSON
【发布时间】:2016-03-03 11:25:07
【问题描述】:

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

【问题讨论】:

    标签: java json spring-mvc serialization


    【解决方案1】:

    要启用从 Camel 案例字段名称(如 firstName)到下划线字段名称(如 field_name)的转换,您应该注册一个自定义 json2Object Conveter 我想您有 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()));
        }
    
    }
    

    2.获取依赖项

    下载这些罐子并将它们放入您的应用程序jackson-annotations-2.7.2,jackson-core-2.7.2,jackson-databind-2.7.2Here is the Maven repository

    此转换器将转换所有带有此标头 Content-Type=application/json 的 REST 消息

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多