仅使用公共字段,如何在@GetMapping
中使用类而不用为其创建getter / setter方法?
@RestController
public class PersonController {
@GetMapping("/person")
public Object search(@Valid Person person) {
//fails
Assert.notNull(person.firstname);
Assert.notNull(person.lastname);
}
}
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Person {
public String firstname, lastname;
}
localhost:8080/person?firstname=john&lastname=doe
我尝试失败的尝试:
@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class Person {}
spring.jackson.visibility.field=any
spring.jackson.visibility.getter=none
spring.jackson.visibility.setter=none
也不起作用的是手动在ObjectMapper
上设置这些字段!