Jackson @JsonProperty create duplicate name

Alexandr Lukovnikov :

I have a class

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.Data;

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
@Table(name = "Sheduler")
public @Data class Lesson {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", updatable = false, nullable = false)
    public int id;

    String state;
    String goal;
    String hobby;
    String result;
    String interest;

    @JsonProperty("fStudentRequest") 
    boolean fStudentRequest;
    @JsonProperty("fTeacherConfirm") 
    boolean fTeacherConfirm;
    @JsonProperty("fStudentConfirm") 
    boolean fStudentConfirm;

    @OneToOne(
            cascade = CascadeType.ALL, 
            orphanRemoval = true,
            fetch = FetchType.LAZY
    )
    @JoinColumn(name = "student_id")
    AuthorisedUser student; 

    @OneToOne(
            cascade = CascadeType.ALL, 
            orphanRemoval = true,
            fetch = FetchType.LAZY
    )
    @JoinColumn(name = "teacher_id")
    AuthorisedUser teacher; 

    @OneToMany(
            cascade = CascadeType.ALL, 
            orphanRemoval = true,
            fetch = FetchType.LAZY
    )
    @JoinColumn(name = "order_id")
    List<Robokassa> robokassa = new ArrayList<>();  

    @OneToMany(
        cascade = CascadeType.ALL, 
        orphanRemoval = true,
        fetch = FetchType.EAGER, 
        mappedBy = "sheduler"
    )
    List<LessonDays> days = new ArrayList<>();  
}

To serialise result I use additional class

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import lombok.Data;
import play.Logger;
public @Data class HttpJsonResponse<T> {


    int status;
    String message;
    List<T> data = new ArrayList<>();
    int code;

    public static <T> String createUserResponse(T data,String message,int code,int status){
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
            HttpJsonResponse<T> response = new HttpJsonResponse<T>();
            response.setStatus(status);
            response.setCode(code);
            response.getData().add(data);
            response.setMessage(message);
            Logger.debug(mapper.writeValueAsString(response));
            return mapper.writeValueAsString(response);
        } catch (JsonProcessingException e) {
            Logger.error(e.toString());
            return "{\"status\":0,\"data\":[],\"code\":\"901\",\"message\":\"error\"}";     
        }
    }
}

When I serialize Lesson class I get duplicated fields

String result = HttpJsonResponse
                .createUserResponse(
                    lessons,
                    "find "+lessons.size(),
                    CODE_OK,
                    STAUS_OK
                );

Sorry for this but I cannot add more code in my post

{
   "status":1,
   "message":"find 1",
   "data":[
      [
         {
            "id":4565,
            "state":"4",
            "goal":"4",
            "hobby":"hobby",
            "result":"result",
            "interest":"4",
            "student":{
               "id":0,
               "email":null,
               "password":null,
               "md5":"e22175516bc91b167e80ceae7276d83b",
               "hash":null,
               "tags":null,
               "emotions":null,
               "balance":0.0,
               "hibernateLazyInitializer":{

               }
            },
            "teacher":{
               "id":0,
               "email":null,
               "password":null,
               "md5":"e22175516bc91b167e80ceae7276d83b",
               "hash":null,
               "tags":null,
               "emotions":null,
               "balance":0.0,
               "hibernateLazyInitializer":{

               }
            },
            "robokassa":null,
            "days":[
               {
                  "id":4558,
                  "from":"10:00",
                  "to":"10:45",
                  "fselect":true,
                  "fSelect":true
               },
               {
                  "id":4559,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               },
               {
                  "id":4560,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               },
               {
                  "id":4561,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               },
               {
                  "id":4562,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               },
               {
                  "id":4563,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               },
               {
                  "id":4564,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               },
               {
                  "id":4566,
                  "from":"10:00",
                  "to":"11:00",
                  "fselect":true,
                  "fSelect":true
               },
               {
                  "id":4567,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               },
               {
                  "id":4568,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               },
               {
                  "id":4569,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               },
               {
                  "id":4570,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               },
               {
                  "id":4571,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               },
               {
                  "id":4572,
                  "from":null,
                  "to":null,
                  "fselect":false,
                  "fSelect":false
               }
            ],
            "fteacherConfirm":true,
            "fstudentConfirm":true,
            "fstudentRequest":true,
            "fStudentRequest":true,
            "fTeacherConfirm":true,
            "fStudentConfirm":true
         }
      ]
   ],
   "code":500
}

What should I do to remove duplicates and get field like fStudentConfirm

Michał Ziober :

These field names are not equal. There is a difference: fteacherConfirm and fTeacherConfirm. Probably, you are using Lambok which generates for you getters and setters. In this specific scenario where field names has one strange letter f Lambok probably creates is-method like below:

public boolean isFstudentRequest() {
    return fStudentRequest;
}

Now, Jackson sees that you have annotation over property and other is-method which has other name it generate two similar (not equal) properties in JSON.

Solutions:

  1. Remove that f at the beginning or add whole word which starts from f.
  2. Remove Lambok and generate is-method manually where you have total control on the generated name.
  3. Disable is-methods in Jackson like below:
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE);

See also:

  1. how to specify jackson to only use fields - preferably globally

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=145088&siteId=1