GroupsDTO
:
@Value
@Jacksonized
@Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class GroupsDTO {
Response response;
@Value
@Jacksonized
@Builder
public static class Response {
int count;
@JsonProperty("items")
List<GroupDTO> groups;
}
}
GroupDTO
:
@Jacksonized
@Builder
@Value
@JsonIgnoreProperties(ignoreUnknown = true)
public class GroupDTO {
int id;
@JsonProperty("is_member")
boolean isMember;
}
I have controller:
@ResponseStatus(HttpStatus.OK)
@GetMapping(value = "/load"/*, produces = MediaType.APPLICATION_NDJSON_VALUE*/)
public Mono<GroupsDTO> loadGroups() {
var group1 = GroupDTO.builder()
.id(1)
.isMember(true)
.build();
var group2 = GroupDTO.builder()
.id(2)
.isMember(false)
.build();
var response = GroupsDTO.Response.builder()
.count(1)
.groups(List.of(group1, group2))
.build();
var groups = GroupsDTO.builder()
.response(response)
.build();
return Mono.just(groups);
}
build.gradle
:
plugins {
id 'org.springframework.boot' version '3.0.0-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.13.RELEASE'
id 'java'
}
// ...
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}
json on perform /load
{"response":{"count":1,"items":[{"id":1,"member":true,"is_member":true},{"id":2,"member":false,"is_member":false}]}}
Where does the member parameter come from ?
CodePudding user response:
In the JavaBeans standard, a boolean property member
would have a getter called isMember()
; a boolean property named isMember
would have a getter called isIsMember()
. Lombok, however(), figures that you probably want isMember()
instead of isIsMember
(a reasonable guess).
You annotated your class with Lombok's @Value
, which auto-creates the get/set methods that formally define a property based on the member fields. Jackson sees your boolean field isMember
, which is annotated with @JsonProperty
, and it renames the JSON property accordingly, but it also sees a method boolean isMember()
, which it interprets as a property named member
, so you get your duplicate output.
Try renaming your field boolean member
to match the JavaBeans conventions.