如何将@GetMapping查询参数绑定到公共字段(没有getter / setter)?

时间:2019-05-29 09:48:33

标签: java spring spring-rest spring-web

仅使用公共字段,如何在@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上设置这些字段!

0 个答案:

没有答案