[英]Where does the member parameter come from?
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;
}
我有 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 執行/加載
{"response":{"count":1,"items":[{"id":1,"member":true,"is_member":true},{"id":2,"member":false,"is_member":false}]}}
成員參數從何而來?
在 JavaBeans 標准中,一個 boolean 屬性member
將有一個名為isMember
的 getter; 名為isMember
的 boolean 屬性將有一個名為isIsMember
的吸氣劑。 但是,Lombok 認為您可能需要isMember
而不是isIsMember
(合理的猜測)。
您使用 Lombok 的@Value
,它會自動創建基於成員字段正式定義屬性的 get/set 方法。 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.
嘗試重命名您的字段boolean member
以匹配 JavaBeans 約定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.