This post has been updated to my personal blog simonting.gize.io


In the actual project development, it is often necessary to deal with the request body from the front desk, such as parameter filtering, parameter verification, illegal parameter interception, parameter decryption and so on. Spring provides the @ControllerAdvice+RequestBodyAdvice solution to process requests globally and avoid service code intrusion at the Controller layer.

== Note: This method only applies to parameters that use the @requestBody annotation, which is essentially AOP and gets attributes in the request header, if requested via GET, such as http://localhost:8080/xx? If id=1, you can’t get the id parameter. Here’s the code to prove it. = =

Code sample

/ * * *@Author zhangting
 * @DescRequest body global uniform processing *@Date2020/07/29 * * /
@Slf4j
@ControllerAdvice(basePackages = "com.example.demo.controller2")
public class GlobalRequestBodyAdvice implements RequestBodyAdvice {
    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class
       > aClass) {
        // Where true indicates that the current advice service is executed, false indicates that the current advice service is not executed
        return true;
    }

    /** * execute ** before reading parameters@param httpInputMessage
     * @param methodParameter
     * @param type
     * @param aClass
     * @return
     * @throws IOException
     */
    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class
       > aClass) throws IOException {
        log.info("-----beforeBodyRead-----");
        return httpInputMessage;
    }

    /** * execute ** after reading the parameters@param o
     * @param httpInputMessage
     * @param methodParameter
     * @param type
     * @param aClass
     * @return* /
    @Override
    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class
       > aClass) {
        log.info("-----afterBodyRead-----");
        log.info("class name is {}",methodParameter.getDeclaringClass().getSimpleName());
        log.info("method name is {}",methodParameter.getMethod().getName());
        log.info("request parameter is {}",o.toString());
        return o;
    }

    /** * No request processing **@param o
     * @param httpInputMessage
     * @param methodParameter
     * @param type
     * @param aClass
     * @return* /
    @Override
    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class
       > aClass) {
        log.info("-----handleEmptyBody-----");
        returno; }}Copy the code

@controllerAdvice Specifies the package path for the current Advice to take effect.

The test class

/ * * *@Author zhangting
* @DescTest *@Date2020/07/29 * * /
@RestController
@RequestMapping("/v2")
@Slf4j
public class TestController2 {

   @RequestMapping(method = RequestMethod.GET, value = "/get")
   publicResponseEntity<? > get(@RequestParam String id) {
       log.info("id is {}", id);
       Map<String,String> rsp = new HashMap<>();
       rsp.put("code"."200");
       rsp.put("msg"."success");
       return new ResponseEntity<>(rsp, HttpStatus.OK);
   }

   @RequestMapping(method = RequestMethod.POST, value = "/post")
   publicResponseEntity<? > post(@RequestBody Map<String, String> map) {
       log.info("begin /v2/post");
       Map<String,String> rsp = new HashMap<>();
       rsp.put("code"."200");
       rsp.put("msg"."success");
       log.info("end /v2/post");
       return newResponseEntity<>(rsp, HttpStatus.OK); }}Copy the code

The test class contains two apis: 1, /v2/get, the request mode is GET 2, /v2/ POST, the request mode is POST through postman test:

Test POST mode:

Conclusion: RequestBodyAdvice applies to the POST +@RequestBody annotation. If the class name, method name, and parameters of the request are successfully obtained, we can perform specific business processing on the parameters.

Test GET method:

Conclusion: RequestAdvice does not work with GET methods.