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
WBOYforward
2023-05-19 18:01:063279browse

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) {
 
    }
}

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){
 
    }
}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete