The way Java receives front-end request bodies

Time:2023-10-14


## The way Java receives front-end request bodies
Request Body (Request Body) is a part of the HTTP request, used to transfer the requested data; in the HTTP request, the request body is usually used for POST, PUT and other request methods that need to transfer data
  • Form Data: The request body is represented as a key-value pair using the&Symbols separate different fields; for example:username=johndoe&password=123456
  • JSON data: the request body is represented in JSON (JavaScript Object Notation) format and is usually used to pass structured data; for example:{"name": "John Doe", "email": "[email protected]"}
  • File upload: the request body is used to transfer the binary data of a file; the file is usually processed using a specific encoding, such as multipart/form-data

@RequestBody

@RequestBody: annotation used in the Spring Framework to receive the front-end request body, you can bind the contents of the request body to a Java object
http://localhost:8080/api/search?code=123
@GetMapping("/search")
    public void  handleRequest(@RequestParam("code") String code) {
        // Processing path parameters
    }

@PathVariable

@PathVariable: Java receives path parameters from front-end requests; path parameters are parts of the URL that exist as placeholders in the API’s URL and can be dynamically fetched and used in Java code
http://localhost:8080/api/example/123
@GetMapping("/example/{id}")
    public void  handleRequest(@PathVariable("id") String id) {
        // Processing path parameters
    }

@RequestParam

@RequestParamNote: Receive query string parameters or form parameters; you can bind the parameter values in the request 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: if or not this parameter is included, default is true: the request path must contain this parameter, otherwise an error will be thrown.
 
// defaultValue: the default value of the parameter, if this value is set, required=true will be invalidated and automatically false, if this parameter is not passed, the default value will be used.
http://localhost:8080/api/example?id=123
@GetMapping("/example")
    public void  handleRequest(@RequestParam(value="id",required="true")) {
        // Processing path parameters
    }
http://localhost:8080/api/example?123
required= false: indicates that the request can be made without this parameter and the method parameter will be set to null
@GetMapping("/example")
    public void  handleRequest(@RequestParam(value="id",required="false")) {
        // Processing path parameters
    }
http://localhost:8080/api/example?id=1
defaultValue="hello": accept parameter is 123 by default, if there is a parameter passed, it is a request parameter.
@GetMapping("/example")
    public void  handleRequest(@RequestParam(value="id",required="false",defaultValue="123")) {
        // Processing path parameters
    }

@Validated

@ValidatedAnnotation: method parameters or method return values are checked

Method parameter validation

Method parameter validation, applied to the controller’s processing methods, verifies that the incoming parameters satisfy the specified validation rules
@Validatedannotation is applied to the method’s parameters, to therequestparameter is calibrated; the rules for the calibration can be defined by adding a new parameter in theRequestDtoUsing annotations on class attributes@NotNull@NotBlanket al. Definitions
@PostMapping("/example")
public void handleRequest(@Validated @RequestBody RequestDto request) {
   // Processing requests
}

Method return value validation

After the execution of the processing method is complete, the method return object can be verified to ensure that the returned data meets the specified verification rules.
@Validatedannotation is applied to the return value of the method, to theResponseDtoobject is calibrated; the calibration rules are passed through theResponseDtoUse annotations on the attributes of the class to define the
@GetMapping("/example/{id}")
public @Validated ResponseDto handleRequest(@PathVariable("id") String id) {
   // Processing requests
   return responseDto;
}

@RequestHeader

@RequestHeaderNote: Receives the value of the request header and can bind the information specified in the request header to the parameters of the method
@GetMapping("/example")
public void handleRequest(@RequestHeader("User-Agent") String userAgent) {
   // Process the value of the request header
}

@HttpServletRequest

@HttpServletRequest annotation: injection in method parametersHttpServletRequestobject, through which the complete request information, including the request body, request header, path parameters and query string parameters, etc., is obtained
@PostMapping("/example")
public void handleRequest(HttpServletRequest request) {
   // Processing request information
}

Conclusion: it’s not easy to create, if you find the blogger’s article pleasing to the eye, please also –pointing and calling (e.g. camera)👍favorite⭐️commentaries📝

Recommended Today

uniapp and applet set tabBar and show and hide tabBar

(1) Set the tabBar: uni.setTabberItem({}); wx.setTabberItem({}); indexnumberisWhich item of the tabBar, counting from the left, is indexed from 0.textstringnoButton text on tabiconPathstringnoImage PathselectedIconPathstringnoImage path when selectedpagePathstringnoPage absolute pathvisiblebooleannotab Whether to display uni.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) wx.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) […]