How to bind request params without setters?

Daniel Olszewski :

I have a simple controller with a GET handler that accepts an object to bind request parameters:

@RestController
@RequestMapping("/test")
public class SampleController {

    @GetMapping
    public SomeResponse find(RequestParams params) {
       // some code
    }

}

The RequestParams is a simple POJO class:

public class RequestParams  {

    private String param1;
    private String param2;

    // constructor, getter, and setters

}

Everthing works fine, but I would like to get rid of the setters to make the object immutable to public use. In the documentation for @RequestMapping handler method up to Spring 5.0.2, we read that possible valid method arguments are:

Command or form objects to bind request parameters to bean properties (via setters) or directly to fields

Is it possible to somehow override the default Spring Boot configuration so that request parameters are bound to class properties using reflection and not with setters?

Update 2018

In the later versions of Spring's documentation, the quoted statement has been rephrased and no longer contain information about binding request parameters directly to fields.

udalmik :

In addition to JSON annotations suggested by @jihor you can try to use custom Web Data binder, adding following code to your controller or to Controller Advice class to span functionality across multiple controllers.

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.initDirectFieldAccess();
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=455008&siteId=1