In Spring, you can inject your custom object to the controller methods like this
Here are the steps of this implementation.
In my sample project I have
ClientRequest object which contains incoming request related variables.
These variables are basically taken from the
HttpServletRequest object. So we could inject
HttpServletRequest instead of
ClientRequest and would generate the same content, but that’s not the point. In your project, you may need custom objects which would need additional object dependencies or you may want to centralize the object creation algorithm instead of repeating it in every controller method.
Next, we implement
ClientRequestArgumentResolver which implements
ClientRequest object is created in the
All we have to do is add a new Configuration class to our project and add this ArgumentResolver to Spring’s argumentResolvers List.
Now we can inject ClientRequest to our controller methods.
The data is displayed in this html file.
Run the app and open the page http://localhost:8080/request