Home >Java >javaTutorial >How to Pass Complex Objects as GET Request Parameters in Spring MVC?

How to Pass Complex Objects as GET Request Parameters in Spring MVC?

DDD
DDDOriginal
2024-11-24 08:14:10329browse

How to Pass Complex Objects as GET Request Parameters in Spring MVC?

Passing Complex Objects as GET Request Parameters in Spring MVC

In a scenario where you're filtering data from a table using Ajax GET requests, you may encounter the need to pass a complex object as a request parameter. Traditionally, this would require a plethora of @RequestParam annotations in your controller.

Problem:

You have a GET request to filter a table with query parameters like:

http://foo.com/system/controller/action?page=1&prop1=x&prop2=y&prop3=z

Your Controller's parameters would be:

@RequestMapping(value = "/action")
public @ResponseBody List<MyObject> myAction(
    @RequestParam(value = "page", required = false) int page,
    @RequestParam(value = "prop1", required = false) String prop1,
    @RequestParam(value = "prop2", required = false) String prop2,
    @RequestParam(value = "prop3", required = false) String prop3) { ... }

Solution:

To simplify this process, you can pass the complex object directly as a request parameter without the @RequestParam annotation. Spring will automatically bind the request parameters to the instance of your class:

public @ResponseBody List<MyObject> myAction(
    @RequestParam(value = "page", required = false) int page,
    MyObject myObject)

The above is the detailed content of How to Pass Complex Objects as GET Request Parameters in Spring MVC?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn