1

I'm using Spring (RESTful API) and I want to have the JSON output of POJOs in lowercase with underscore instead of camelcase:

e.g.: 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;
    }
}

So imageUrl should be image_url (also for the incoming objects -> mapping)

Is there a way to have this configuration globally? (Don't want to use JsonProperty annotation)

My 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>
kellyfj
  • 6,586
  • 12
  • 45
  • 66
Fabian Lurz
  • 2,029
  • 6
  • 26
  • 52
  • What do you mean with `to have the output of POJOs in lowercase with underscore`? The name of the variable `imageUrl` changed to `image_url`? If so. You should avoid that [Oracle - tutorial variable naming](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html#Naming). – SubOptimal Apr 06 '16 at 12:10
  • I mean Json output. Sry that was unclear. I just want to have another format for my Json output then the POJO originally has. So imageUrl should be image_url in the Json output. – Fabian Lurz Apr 06 '16 at 12:15

1 Answers1

3

You need to configure Jackson ObjectMapper used by Spring's Jackson http message converter to use a different property naming strategy.

objectMapper.setPropertyNamingStrategy(new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy())

You can set this globally in your XML or java config. How exactly to do that depends on Spring MVC version, whether you're using XML config or Java config, whether you're using Spring Boot or regular Spring MVC etc. There are many questions on stack overflow that offer answers for different combinations.

One of the ways to do it in XML config would be this:

    <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>
Krešimir Nesek
  • 5,302
  • 4
  • 29
  • 56
  • Thanks -> can you tell me how to do that in XML? ;) – Fabian Lurz Apr 06 '16 at 12:25
  • It will really depend on Spring version used and bits and pieces of your config. Here's a question with examples for several Spring versions: http://stackoverflow.com/questions/7854030/configuring-objectmapper-in-spring – Krešimir Nesek Apr 06 '16 at 12:31