0

The fields have a mismatch in their case. I feel that I'm missing something when the POJO fields are converted to JSON response. The Jackson serialization is not setting the properties correctly

My JSON response looks like this 
[{"drivers":0,"next_DT":"May 18 2019  2:00AM"}]    

Expected 
[{"DRIVERS":0,"NEXT_DT":"May 18 2019 2:00AM"}]    

Below is my POJOClass where I'm mapping the results of the native query

public class GetTrucksDueResultModel 
{

     private int DRIVERS;

     private String NEXT_DT;

public GetTrucksDueResultModel()
{

}

public GetTrucksDueResultModel(String NEXT_DT,int DRIVERS)
{
    this.NEXT_DT = NEXT_DT;
    this.DRIVERS = DRIVERS;
}
public int getDrivers() {
    return DRIVERS;
}

public void setDrivers(int dRIVERS) {
    DRIVERS = dRIVERS;
}

public String getNEXT_DT() {
    return NEXT_DT;
}

public void setNEXT_DT(String nEXT_DT) {
    NEXT_DT = nEXT_DT;
}

}   

And my sqlMapping in the entity class looks like this

@SqlResultSetMapping(name="GetTrucksDueResultMapping",
                   classes={
                   @ConstructorResult(targetClass=GetTrucksDueResultModel.class,
                  columns={
                           @ColumnResult(name="NEXT_DT", type=String.class),
                           @ColumnResult(name="DRIVERS", type=int.class)
                        })
})

Below are the dependencies in my pom xml.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    <!--<dependency> <groupId>jconn4</groupId> <artifactId>jconn4</artifactId> 
        <version>1.0</version> </dependency> -->
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-sql</artifactId>
        <version>${querydsl.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
</dependencies>    

2 Answers2

0

Jackson uses the getters to serialize the state. That's why the JSON attributes are lower case.

If you want to use the attributes you have to configure Jackson like this:

@Configuration
public class JsonConfig {

    @Bean
    public Jackson2ObjectMapperBuilder objectMapperBuilder() {

        return new Jackson2ObjectMapperBuilder() {

            @Override
            public void configure(ObjectMapper objectMapper) {
                super.configure(objectMapper);
                objectMapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
                objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
            }
        };
    }
}

This setts the PropertyAccessor on the fields instead of the getters and setters.

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
0

Adding @JsonGetter("NEXT_DT") and @JsonGetter("DRIVERS") anotations on top of respective getters worked for me. But not a good idea if you have many fields.