Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind @RequestParam to object in spring MVC?

I want to make a POST request through AJAX, and I also want to bind the whole class object to the request, and I want to receive that request with @requestParam annotation. I know it can be done with @requestBody annotation, but I am curious to know: can we do it with @requestParam annotation?

An Ajax code:

var restDTO{
    id: 3,
    name: "hello"
}

 $.ajax({
          url: url, 
          type: 'POST',
          dataType: 'json',
          contentType: 'application/json',
          mimeType: 'application/json',
          data: JSON.stringify({RestDTO : restDTO}),          
          success: function(data) 
    {
    }

I do have RestDTO

Class RestDTO 
{

    int id;
    String name;

    //getter and setter

}

In controller

public String content(@RequestParam RestDTO restDTO){...}

What should I do the make this code run?

What should I change in sending data from ajax?

Do I need to change on server to receive an RestDto object with @requestParam annotation?

like image 747
user3029929 Avatar asked Mar 15 '16 05:03

user3029929


People also ask

Can RequestParam take object?

You cannot use the @RequestParam annotation inside objects but it doesn't mean you're left with no other solution. In this post, I'm going to show you how to bind multiple request parameters to an object in Spring application.

What is the use of @RequestParam in Spring MVC?

In Spring MVC, the @RequestParam annotation is used to read the form data and bind it automatically to the parameter present in the provided method. So, it ignores the requirement of HttpServletRequest object to read the provided data.

Can I use @RequestParam annotation for a post request?

Simply put, we can use @RequestParam to extract query parameters, form parameters, and even files from the request.

Can we use @RequestParam in Get method?

Using @RequestParamTo see @RequestParam in action, we'll write a @GetMapping method in a Spring @RestController and execute the endpoint with requests contenting query parameters.


2 Answers

You can't, because @RequestParam just indicates, that one method's parameter should be bound to a one web request's parameter. It can't do mapping to objects. For use @RequestParam you should change ajax request:

var restDTO{
   id: 3,
   name: "hello"
}

 $.ajax({
          url: url, 
          type: 'POST',
          data: restDTO,          
          success: function(data){
             ....
          }
});

JQuery will send request as application/x-www-form-urlencoded and will process data to parameters automatically. You controller's method should look like following:

@RequestMapping("/url")
public String content(@RequestParam Long id, @RequestParam String name){...}

For automatically map parameters to object you can use @ModelAttribute annotation:

@RequestMapping("/url")
public String content(@ModelAttribute RestDTO restDTO){...}

In this case, names in javascript map should match to names of properties in RestDTO.

Generally, @ModelAttribute and @RequestBody created for same purposes: for binding data from request to method (whether objects of primitive type).

I consider, that @ModelAttribute is more convenient, when you working with html-forms and plain objects. There is ready to use Spring abilities like modelAttribute and path.

In its turn, @RequestBody more flexible, when you need manual control over data. Also, it is more convenient, when you're working with complex objects.

Personally me would prefer @RequestBody and json.

like image 50
Ken Bekov Avatar answered Nov 08 '22 04:11

Ken Bekov


If you're sending your data as classic request params, you can bind to object by simply omitting the @RequestParam, so

public String content(RestDTO restDTO){...}

If you're sending json, you have to use @RequestBody.

If whysoever you're insisting on the @RequestParam, note that you can bind multiple values against a map, so

public String content(@RequestParam Map<String, String> restDTO){...}

From the @RequestParam doc

If the method parameter is Map or MultiValueMap and a parameter name is not specified, then the map parameter is populated with all request parameter names and values.

like image 25
Master Slave Avatar answered Nov 08 '22 05:11

Master Slave