SoFunction
Updated on 2025-03-01

Summary of various ways Java receives front-end request bodies

Preface

The request body is part of the HTTP request and is used to transmit the requested data; in HTTP requests, the request body is usually used for request methods such as POST and PUT that require data transmission.

  • Form Data: The request body is represented in the form of key-value pairs, using&Symbols separate different fields; for example:username=johndoe&password=123456
  • JSON data: The request body is represented in JSON (JavaScript Object Notation) format, which is usually used to pass structured data; for example:{"name": "John Doe", "email": "johndoe@"}
  • File upload: The request body is used to transfer binary data of the file; the file is usually processed using a specific encoding method, such as multipart/form-data

@RequestBody

@RequestBody: The annotation used in the Spring framework to receive front-end request body, and the content of the request body can be bound to Java objects

http://localhost:8080/api/search?code=123
@GetMapping("/search")
    public void  handleRequest(@RequestParam("code") String code) {
        // Process path parameters    }

@PathVariable

@PathVariable: Receive path parameters in front-end requests in Java; path parameters refer to part of the URL, which exist in the API URL in the form of placeholders and can be dynamically obtained and used in Java code

http://localhost:8080/api/example/123
@GetMapping("/example/{id}")
    public void  handleRequest(@PathVariable("id") String id) {
        // Process path parameters    }

@RequestParam

@RequestParamNote: Receive query string parameters or form parameters; the parameter values ​​in the request can be bound to the parameters of the method; this method is suitable for obtaining the value of a specific parameter.

@RequestParam(value="Parameter name",required="true/false",defaultValue="")
// value: parameter name// required: Whether to include this parameter, default is true: The request path must include this parameter, otherwise an error will be reported// defaultValue:Default parameter value,If this value is set,required=trueWill expire,Automaticallyfalse,If this parameter is not passed,Use the default value
http://localhost:8080/api/example?id=123
@GetMapping("/example")
    public void  handleRequest(@RequestParam(value="id",required="true")) {
        // Process path parameters    }
http://localhost:8080/api/example?123
required= false:It means that this parameter may not be present in the request,The method parameters will be set to null
@GetMapping("/example")
    public void  handleRequest(@RequestParam(value="id",required="false")) {
        // Process path parameters    }
http://localhost:8080/api/example?id=1
defaultValue="hello":Accepted parameters default to123,If the parameter is passed, the request parameter is
@GetMapping("/example")
    public void  handleRequest(@RequestParam(value="id",required="false",defaultValue="123")) {
        // Process path parameters    }

@Validated

@ValidatedNote: Method parameters or method return value are checked

Method parameter verification

Method parameter verification, applied to the controller's processing method, verify whether the incoming parameters meet the specified verification rules

@ValidatedThe annotation is applied to the parameters of the method,requestThe parameters are checked; the verification rules can be passed inRequestDtoUse annotations on the properties of the class@NotNull@NotBlankDefinitions

@PostMapping("/example")
public void handleRequest(@Validated @RequestBody RequestDto request) {
   // Process the request}

Method return value verification

After the execution of the processing method, you can verify the return object of the method to ensure that the returned data meets the specified verification rules.

@ValidatedThe annotation is applied to the return value of the method,ResponseDtoThe object is checked; the verification rules are passedResponseDtoUse annotations to define the properties of the class

@GetMapping("/example/{id}")
public @Validated ResponseDto handleRequest(@PathVariable("id") String id) {
   // Process the request   return responseDto;
}

@RequestHeader

@RequestHeaderNote: Receive the value of the request header and bind the information specified in the request header to the parameter of the method.

@GetMapping("/example")
public void handleRequest(@RequestHeader("User-Agent") String userAgent) {
   // Process the value of the request header}

@HttpServletRequest

@HttpServletRequest annotation: Injected into method parametersHttpServletRequestObject, through which to obtain complete request information, including request body, request header, path parameters and query string parameters, etc.

@PostMapping("/example")
public void handleRequest(HttpServletRequest request) {
   // Process request information}

Summarize

This is the end of this article about Java receiving front-end request bodies. For more related content on Java receiving front-end request bodies, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!