zeodtr
zeodtr

Reputation: 11275

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

I am writing following code:

@RestController
@RequestMapping("/user")
public class UserController
{
    @Autowired private JdbcTemplate jt;

    @RequestMapping("/getUsers")
    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?

Upvotes: 3

Views: 8044

Answers (1)

M. Deinum
M. Deinum

Reputation: 124526

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

    @Value("${userList.pageSize:500}")
    private int pageSize;

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

Upvotes: 3

Related Questions