2016-04-06 94 views
0

我使用Spring(REST的API),我想擁有的POJO的JSON輸出小寫以下劃線代替駝峯:隱蔽首字母大寫小寫以下劃線春天

如: PhotoUpload:

package com.rh.dao.entity; 

/** 
* 
* @author fabianlurz 
*/ 
public class PhotoUpload { 
    private String imageUrl;  

    /** 
    * @return the imageUrl 
    */ 
    public String getImageUrl() { 
     return imageUrl; 
    } 

    /** 
    * @param imageUrl the imageUrl to set 
    */ 
    public void setImageUrl(String imageUrl) { 
     this.imageUrl = imageUrl; 
    } 
} 

所以IMAGEURL應該IMAGE_URL(也爲進入對象 - >映射)

有沒有一種方法來在全球擁有這個配置? (不想使用JsonProperty註釋)

我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.javahonk</groupId> 
    <artifactId>migranthire_api_v2.0</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>migranthire_api_v2.0</name> 
    <url>http://maven.apache.org</url> 
    <properties> 
     <junit.version>3.8.1</junit.version> 
     <SpringVersion>4.0.6.RELEASE</SpringVersion> 
     <spring-jdbc.version>4.0.6.RELEASE</spring-jdbc.version> 
     <json.version>20140107</json.version> 
     <jackson.version>1.9.10</jackson.version> 
     <log4j.version>1.2.16</log4j.version> 
     <jtds.version>1.2</jtds.version> 
     <net.sf.json.version>2.4</net.sf.json.version> 
     <spring.security.version>4.0.2.RELEASE</spring.security.version> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>${junit.version}</version> 
      <scope>test</scope> 
     </dependency> 
     <!-- Spring dependencies --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-core</artifactId> 
      <version>${SpringVersion}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>${SpringVersion}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${SpringVersion}</version> 
     </dependency> 
     <!-- Spring and Transactions --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-jdbc</artifactId> 
      <version>${spring-jdbc.version}</version> 
     </dependency> 

     <!-- Jackson JSON Mapper --> 
     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>${jackson.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.json</groupId> 
      <artifactId>json</artifactId> 
      <version>${json.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>net.sourceforge.jtds</groupId> 
      <artifactId>jtds</artifactId> 
      <version>${jtds.version}</version> 
     </dependency> 
     <!-- MySql 5.5 Connector --> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.29</version> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>${log4j.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>com.zaxxer</groupId> 
      <artifactId>HikariCP-java6</artifactId> 
      <version>2.3.9</version> 
     </dependency> 
     <dependency> 
      <groupId>net.sf.json-lib</groupId> 
      <artifactId>json-lib</artifactId> 
      <version>${net.sf.json.version}</version> 
      <classifier>jdk15</classifier> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-core</artifactId> 
      <version>${spring.security.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <version>3.1.0</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp.jstl</groupId> 
      <artifactId>jstl-api</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>javax.servlet.jsp-api</artifactId> 
      <version>2.3.1</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-validator</groupId> 
      <artifactId>commons-validator</artifactId> 
      <version>1.4.0</version> 
     </dependency>  
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-web</artifactId> 
      <version>4.0.4.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
      <version>4.0.4.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>com.cloudinary</groupId> 
      <artifactId>cloudinary-http44</artifactId> 
      <version>1.4.1</version> 
     </dependency> 
    </dependencies> 
    <build> 
     <finalName>migranthire_api_v2.0</finalName> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
       <version>3.1</version> 
      </plugin> 
     </plugins> 
    </build> 
</project> 
+0

你是什麼意思與'擁有的POJO的小寫的輸出與underscore'?變量'imageUrl'的名稱更改爲'image_url'?如果是這樣。您應該避免使用[Oracle - 教程變量命名](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html#Naming)。 – SubOptimal

+0

我的意思是Json輸出。 Sry說不清楚。我只想爲我的Json輸出使用另一種格式,然後是POJO原來的格式。所以imageUrl應該是Json輸出中的image_url。 –

回答

3

您需要配置使用Spring的傑克遜HTTP消息變換使用不同屬性的命名策略傑克遜ObjectMapper。

objectMapper.setPropertyNamingStrategy(new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy()) 

您可以在XML或java配置文件中全局設置它。如何做到這一點取決於Spring MVC版本,無論您是使用XML配置還是Java配置,無論您是使用Spring Boot還是普通的Spring MVC等。堆棧溢出問題都有很多問題,可以爲不同的組合提供答案。

一個做到這一點的XML配置會是這樣的方式:

<mvc:annotation-driven> 
     <mvc:message-converters register-defaults="true"> 
      <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
       <property name="objectMapper"> 
        <ref bean="objectMapper" /> 
       </property> 
      </bean> 
     </mvc:message-converters> 
    </mvc:annotation-driven> 


    <bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" autowire="no"> 
     <property name="propertyNamingStrategy" value="CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES" /> 
    </bean> 
+0

謝謝 - >你能告訴我如何在XML中做到這一點? ;) –

+0

它將真正取決於所使用的Spring版本和你的配置的一些部分。下面是幾個Spring版本的例子的一個問題:http://stackoverflow.com/questions/7854030/configuring-objectmapper-in-spring –