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.