Spring Boot and MVC: How to set default value for @RequestBody object fields from application.properties?

By : zeodtr
Source: Stackoverflow.com

I am writing following code:

public class UserController
    @Autowired private JdbcTemplate jt;

    public ListResult getUsers(@RequestBody GetUsersArgs args)
        // paging query that returns ListResult object.
        // ...

    private static class GetUsersArgs
        public int firstRowIndex = 0;
        public int pageSize = 500;

What I want to do is, to set the default value for pageSize field of GetUsersArgs object from application.properties file of Spring Boot.

When the content of application.properties file is as follows,

server.port: 9000
management.port: 9001
userList.pageSize: 100

pageSize field must be set to 100. Otherwise, set to 500.

How can it be done?

By : zeodtr


It isn't going to work that way. Spring can only replace values in managed beans, yours is constructed from request parameters. What you can do is inject a value into your controller and use that

public class UserController

    private int pageSize;

This value you can then use in your method to set the pageSize on the object.

By : M. Deinum

This video can help you solving your question :)
By: admin