8 Ways Spring Boot Receives Parameters
This is a summary of how Spring Boot can receive parameters from HTTP requests.
How that will become one of your library stories.
1 . Formal Parameters
This approach is generally suitable for cases with fewer parameters.
@Slf4j
@RestController
@RequestMapping("/profile")
public class ProfileController {
@GetMapping("/detail")
public String getDetail(String uid) {
log.info("uid:{}", uid);
return "ok";
}
}
if the parameter is labeled with @RequestParam
, indicating that this parameter needs to be passed, otherwise an error will be reported.
@Slf4j
@RestController
@RequestMapping("/profile")
public class ProfileController {
@GetMapping("/detail")
public String getDetail(@RequestParam String uid) {
log.info("uid:{}", uid);
return "ok";
}
}
2. Entity Parameters
@Slf4j
@RestController
@RequestMapping("/profile")
public class ProfileController {
@GetMapping("/detail")
public String getDetail(Profile profile) {
log.info("profile:{}", profile);
return "ok";
}
}
The RequestParam
annotation cannot be used when a Get
request receives a parameter from an entity class, as it is not supported to get the parameter in this way.
3. Via HttpServletRequest
@RestController
@RequestMapping("/profile")
@Slf4j
public class ProfileController {
@GetMapping("/detail")
public String getDetail(HttpServletRequest request) {
String name = request.getParameter("name");
String sex = request.getParameter("sex");
log.info("name:{}", name);
log.info("sex:{}", sex);
return "ok";
}
}
4. Via the @PathVariable annotation
@RestController
@RequestMapping("/profile")
@Slf4j
public class ProfileController {
@GetMapping("/detail/{name}/{sex}")
public String getDetail(@PathVariable String name, @PathVariable String sex) {
log.info("name:{}", name);
log.info("sex:{}", sex);
return "ok";
}
}
5. Array Parameters
@RestController
@RequestMapping("/profile")
@Slf4j
public class ProfileController {
@GetMapping("/detail")
public String getDetail(String[] names) {
Arrays.asList(names).forEach(name->{
System.out.println(name);
});
return "ok";
}
}
6. Set Parameters
@RestController
@RequestMapping("/profile")
@Slf4j
public class ProfileController {
@GetMapping("/detail")
public String getDetail(@RequestParam List<String> names) {
names.forEach(name->{
System.out.println(name);
});
return "ok";
}
}
7. Via the @RequestBody annotation
The RequestBody
annotation is used to receive json format parameters in the body passed from the frontend.
@RestController
@RequestMapping("/profile")
@Slf4j
public class ProfileController {
@PostMapping("/save")
public String getDetail(@RequestBody Profile profile) {
log.info("name:{}", profile.getName());
log.info("phone:{}", profile.getPhone());
return "ok";
}
}
8. Via Map
1. Pass the parameter as param, the
RequestParam
annotation receives the parameter.
@RestController
@RequestMapping("/profile")
@Slf4j
public class ProfileController {
@PostMapping("/save")
public String getDetail(@RequestParam Map<String,Object> map) {
System.out.println(map);
System.out.println(map.get("name"));
return "ok";
}
}
2. Pass the parameter in body
JSON
format and receive it inRequestBody
annotation.
@RestController
@RequestMapping("/profile")
@Slf4j
public class ProfileController {
@PostMapping("/save")
public String getDetail(@RequestBody Map<String,Object> map) {
System.out.println(map);
System.out.println(map.get("name"));
return "ok";
}
}
See you next time! Thanks for reading.