1

I use Spring Boot 2.x and maven

  • pom.xml
     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </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-data-rest</artifactId>
        </dependency>

         <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
  • json, That is sending.
{
  "name": "fan",

  "isCar": true,

  "year": 2020,

  "character": "W",

  "cost": 10,

  "status": "OPEN"
}
  • a part of DTO
public abstract class DifferentTypesDtoFour extends DifferentTypesDtoTwo {

    private Integer year;

    private boolean isCar;

    public DifferentTypesDtoFour() {
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public boolean isCar() {
        return isCar;
    }

    public void setCar(boolean car) {
        isCar = car;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;
        DifferentTypesDtoFour that = (DifferentTypesDtoFour) o;
        return isCar == that.isCar &&
                Objects.equals(year, that.year);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), year, isCar);
    }

    @Override
    public String toString() {
        return super.toString() + "\n" + "DifferentTypesDtoFour{" +
                "year=" + year +
                ", isCar=" + isCar +
                '}';
    }
}

I can 't understand the origin of this problem.

When the request comes to the rest controller , the dto receives the converted data.

Almost data is converted the correct, but the field isCar = false...

Why ?

Does anyone have any ideas how to fix this?

Update

I used the following:


public abstract class DifferentTypesDtoFour extends DifferentTypesDtoTwo {

    private Integer year;

    private boolean isCar;

    public DifferentTypesDtoFour() {
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    @JsonProperty(value = "isCar")
    public boolean isCar() {
        return isCar;
    }

    @JsonProperty(value = "isCar")
    public void setCar(boolean car) {
        isCar = car;
    }
...

}

The type boolean saved indeed, but when the data is back to rest-controller, the client receives a response and the type boolean again don't transform correct.

Gimby
  • 5,095
  • 2
  • 35
  • 47
skyho
  • 1,438
  • 2
  • 20
  • 47
  • Check the answer here:https://stackoverflow.com/questions/32270422/jackson-renames-primitive-boolean-field-by-removing-is – Sebastian Mar 05 '20 at 15:08
  • I rolled back the last edit. Please don't edit solutions into the question. Solutions are only to be given in answers. – Gimby Mar 06 '20 at 14:54

1 Answers1

2

The problem is with your setter of isCar field - the setter method's name should be setIsCar instead of setCar so that the request json's isCar value can be mapped into your model's isCar property,

public void setIsCar(boolean isCar) {
    this.isCar = isCar;
}

Edit 1... As @Gimby mentioned that your getter name should be isIsCar, it will not affect while getting HTTP request but it will eventually look unmatched getter for that field - and you may consider re-naming your variable to avoid such typo issues.

Edit 2... One of the ways to get rid of the issue is using @JsonProperty without renaming methods as mentioned by @GabLeg,

@JsonProperty(value="isCar")
public void setCar(boolean car) {
    this.isCar = car;
}
Shekhar Rai
  • 2,008
  • 2
  • 22
  • 25
  • 1
    the getter is also wrong in the same way, should then get the funky name isIsCar(). – Gimby Mar 05 '20 at 16:20
  • 1
    One way to get rid of the funky name like `isIsCar()` is to add the annotation `@JsonProperty("isCar")`. – GabLeg Mar 05 '20 at 17:27