2016-03-03 160 views
1

我一直在處理一個控制器,我想在其中返回json字符串作爲響應。但問題是,我想在serialize/deserialize期間更改一些字段名稱,但我不想在實體對象上使用醜陋的註釋。JSON在Spring MVC上使用不同的字段名序列化

比方說

@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

採取例如爲了實現從駱駝情況下,轉型字段的名稱,如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。如果您使用的編程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依賴

下載這些罐子並把它們在你的應用程序jackson-annotations-2.7.2jackson-core-2.7.2jackson-databind-2.7.2Here is the Maven repository

這個轉換器將所有的REST消息轉換與此標頭Content-Type=application/json

PS:此轉換器不會將您的json消息轉換爲字符串,因爲字符串沒有默認構造函數,可以讀取您的JSON消息作爲您的控制器中的字符串,您在客戶端消息頭中使用Content-Type = applciation/text

相關問題