Spring MVC: Complex Object as Get @Requestparam

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



Leave a reply



Submit