spring MVC Practice Notices

  • 2021-09-05 00:00:31
  • OfStack

Parameter verification of directory request: For parameters in url, for parameters in request and body request body, response data format: Global exception handling system 1 defines exception response code for easy viewing

Using spring mvc to provide restful style interface, the front and back ends are separated, and the back end only provides data interface without page rendering. Therefore, the interface can be treated as a whole:

Request parameter verification Response data format Global exception handling

Through the above system 1 processing, developers can focus on business processing and separate the logic of verification and exception response.

Request parameter validation:

For parameters in url

If @ RequestParam is added but the parameter is not passed, an MissingServletRequestParameterException exception is thrown If annotations such as @ Max are added and this condition is not met, an ConstraintViolationException or BindException exception is thrown

For parameters in the request body request body

Add @ RequestBody before Bean and throw an HttpMessageNotReadableException exception if there is no request body Add @ Validate annotation before bean, and add @ Max, @ NotNull and other annotations to the attributes in bean, that is, verify the response attributes. If it is not satisfied, throw MethodArguementNotValidException group can be added to @ Validate to indicate the operation to be verified. groups can be added to @ Max, and when groups contains group, verification is performed. Bean can also customize annotations, through the implementation of ConstraintValidator interface custom verification logic. If validation fails, an MethodArgumentNotValidException exception is thrown,

These exceptions are intercepted by global exception handling, which returns exception information.

Response data format:

Establishment of Unified 1 Response Class JsonResp < T > Generic T is the service response content, which can be String, List, java bean and so on. The definition attribute result indicates whether the response was successful, the response code code, and the response code information msg. Here code and msg correspond to the response enumeration class for global exception handling. Defining ResponseBodyhandler to implement ResponseBodyAdvice interface, when the response type is json, if the response type is JsonResp, directly respond to the client; If not, then new JsonResp, the response body set into the response to the client. String, List, java and bean can be defined arbitrarily in the method return type of controller, and ResponseBodyhandler can be transformed into the form of JsonResp. If it is a stream file, the direct return will not be converted into JsonResp form. In this way, the response can be packaged into JsonResp data format of Series 1, which is convenient for front-end processing.

Global exception handling

Establish a response enumeration class ResponseEnum, and define the response code and response code information required by the service Establish custom exception class BaseException, inherit runtime exception RuntimeException, and set ResponseEnum during initialization. Create exception catching class GlobalExceptionAspect, add @ RestCOntrollerAdvice annotation to the class, use @ Exceptional catching parameter to validate exception and custom exception BaseException. The outermost layer catches Exception and catches undefined exceptions If a service error is reported, throw new BaseException (ResponseEnum. xxx) throws a custom exception, which is captured by the GlobalExceptionAspect section and responds to the client

Uniform 1 defines abnormal response code for easy viewing

Avoid abnormal response in business code and simplify code

The essence of these unified 1 processes is to use fliter or aop to intercept request and response parameters for general logic processing. Therefore, requests can be processed such as sensitive word filtering, parameter encryption and decryption, and business logic can be decoupled.

The above is the details of spring MVC practice. Please pay attention to other related articles on this site for more information about spring MVC practice!


Related articles: