Home > Java > javaTutorial > How does SpringBoot use entities to receive multiple parameters passed by the Get request?

How does SpringBoot use entities to receive multiple parameters passed by the Get request?

WBOY
Release: 2023-05-19 18:01:06
forward
3286 people have browsed it

1. The Controller layer receives parameters without any annotations

The first method is the simplest. I seriously doubt why it was not used before. Who knows. . But this time it really worked. The simplest way is to add the Controller interface without adding any annotations! ! ! SpringBoot handles this automatically. The code is as follows:

/**
 * @author zhangzhixiang
 * @since v1.0.0
 */
@RestController
@RequestMapping(path = "/ui/institution")
public class InstitutionManagementController {
 
    @GetMapping(value = "/pageQueryForAssign")
    public void pageQueryInstitutionsForAssign(InstitutionQueryDTO queryDTO) {
 
    }
}
Copy after login

In fact, the point is that there are no annotations next to InstitutionQueryDTO, so that the front-end can pass Get parameters normally. An example of the front-end parameter passing format is as follows:

http://192.168.63.125/ui /institution/pageQueryForAssign?name='xxx'&sex='Male'

The name and sex here are attributes in the InstitutionQueryDTO entity, and SpringBoot will automatically fill them into the entity for us.

2. The Controller layer receives parameters through @ModelAttribute

This writing method was found by reading articles on the Internet. I will also record this method.

/**
 * @author zhangzhixiang
 * @since v1.0.0
 */
@RestController
@RequestMapping(path = "/ui/institution")
public class InstitutionManagementController {
 
    @GetMapping(value = "/test")
    public void test(@ModelAttribute InstitutionQueryDTO queryDTO){
 
    }
}
Copy after login

The focus here is the @ModelAttribute annotation, which will also fill the parameters passed from the front end into the business entity. The front-end parameter passing format is the same as method one.

I should have used the first method to accept the parameters of the Get request a year ago, but it failed and did not receive it. The reason for my failure should be that my Controller received multiple entity input parameters at the same time, so it failed. .

The above is the detailed content of How does SpringBoot use entities to receive multiple parameters passed by the Get request?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template