Spring MVC: Complex object as GET @RequestParam
You can absolutely do that, just remove the @RequestParam
annotation, Spring will cleanly bind your request parameters to your class instance:
public @ResponseBody List<MyObject> myAction(
@RequestParam(value = "page", required = false) int page,
MyObject myObject)
Spring MVC: Complex object with an array field as GET @RequestParam
I was able to fix this issue by changing val
to var
in the FilterRequest class
Spring MVC: Complex object as parameter
since you are trying to deal with a complex object accept your object from a post request.
@RequestMapping("/",method=RequestMethod.POST)
public String getMessageInfo(@RequestBody Message message) {
return message;
}
in the above code i'm setting method
attribute to POST
then it will be called when you are making a POST
request, and i am using @RequestBody Message message
inside the method parameter
. which will convert and form an Message object
from the incoming request
, if you dont put @requestBody
annotation then a Bean will be injected
to the method by spring instead of forming a one from the request.
you can try this code to make the request
final String uri = "http://localhost:8080/";
Message message = new Message(1, "Adam",true);
RestTemplate restTemplate = new RestTemplate();
Message result = restTemplate.postForObject( uri, message, Message.class);
when making an request create an Message object
setting each and every field in it, otherwise you will end up in having Bad request
error.
spring MVC request param auto mapping complex object
If you do things like this:
public @ResponseBody List<MyObject> myAction(@RequestBody MyObject myObject) { ... }
Of course you can only have one body in your http request.
As long as you have Jackson in your classpath (spring boot will add this automatically) your objects will be marshalled correctly.
If the JSON in your body is incorrect you will get a 400 (Invalid Request) returned.
Clean way to bind @RequestParam to an object instead of a simple type
You can bind request parameters to custom model objects in Spring like this:
public class Page {
private int pageOffset;
private int pageSize;
// Getters and setters omitted
}
@RequestMapping("/people")
public void findAll(Page page) {
}
If you send request like GET /foo?pageOffset=0&pageSize=25
then the page
parameter will be populated with those values. Notice also that there is no @RequestParam
annotation.
Resolved Page
object will be provided to the handler by ServletModelAttributeMethodProcessor
under the hood, so feel free to look at its source code for more details.
If you need more flexibility in customizing the resolution of paging parameters, you can implement custom HandlerMethodArgumentResolver
. You may use PageableHandlerMethodArgumentResolver
from Spring Data JPA as an inspiration if you decide to go this way (or use Spring Data JPA for your persistence layer).
Spring MVC @RequestParam a list of objects
Request parameters are a Multimap of String to String. You cannot pass a complex object as request param.
But if you just pass the username that should work - see how to capture multiple parameters using @RequestParam using spring mvc?
@RequestParam("users") List<String> list
But I think it would be better to just use the request body to pass information.
Related Topics
How to Put a Jar in Classpath in Eclipse
Swagger Ui Is Not Loading Swagger API Json
Use Mockito/Powermockito to Mock a Public Method of Super Class
Interview Question: Check If One String Is a Rotation of Other String
How to Get the Intersection Between Two Arrays as a New Array
Why We Use If, Else If Instead of Multiple If Block If the Body Is a Return Statement
What Is More Efficient, I++ or ++I
How to Use a Delimiter with Scanner.Usedelimiter in Java
Remove All Occurrences of Char from String
JSON Java 8 Localdatetime Format in Spring Boot
Explicitly Calling a Default Method in Java
How to Call a Static Method in Jsp/El
How to Read and Copy the Http Servlet Response Output Stream Content for Logging
How to Handle Calendar Timezones Using Java
How to Run .Jar File by Double Click on Windows 7 64-Bit
Jaxb: How to Marshall Map into <Key>Value</Key>
Parse JSON from Httpurlconnection Object
By Default, Jsf Generates Unusable Ids, Which Are Incompatible with the CSS Part of Web Standards