编程知识 cdmana.com

Design and implementation of response standard in 12 springboot project

12-SpringBoot Design and implementation of response standard in engineering

springboot

Published on 10 month 12 Japan

Background analysis

Based on C/S In the programming model of Architecture , The client often needs the data returned by the server , Different processing based on different states . for example , Correct state data, a way to present it , Wrong state data is another way to present it . So the standardized design of server response data came into being .

Response to standard design

In response to data standardization design , The first step is to analyze the response data , What data to respond to the client , How to design the state of these data . Suppose that the business data you are responding to now contains three parts : state , news , Specific data . We can design it like this , for example :

package com.cy.pj.common.pojo;

/**
 *  Based on this object, encapsulate the data that the server responds to the client 
 */
public class ResponseResult {
    /** Response status code ( Some people use code)*/
    private Integer state=1;//1 Express ok,0 Express error,.....
    /** Information corresponding to the status code */
    private String message="ok";
    /** Correct response data */
    private Object data;

    public ResponseResult(){}

    public ResponseResult(String message){//new ResponseResult("delete ok"),
        this.message=message;
    }
    public ResponseResult(Object data){//new ResponseResult(list);
        this.data=data;
    }
    public ResponseResult(Throwable e){//new ResponseResult(e);
        this.state=0;
        this.message=e.getMessage();
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}

Encapsulation of response data

stay Controller Class logic method for normal response data encapsulation , for example :

package com.cy.pj.module.controller;

import com.cy.pj.common.pojo.ResponseResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ArithmeticController {

      @RequestMapping("/doCompute/{n1}/{n2}")
      public ResponseResult doCompute(@PathVariable  Integer n1, @PathVariable Integer n2){
          Integer result=n1/n2;
          ResponseResult r=new ResponseResult(" The result of the calculation is :"+result);
          r.setData(result);
          return r;
      }
}

Encapsulate the exception response data in the global exception handling object , for example :

package com.cy.pj.common.web;

import com.cy.pj.common.pojo.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestControllerAdvice
public class GlobalExceptionHandler {
      private static final Logger log= LoggerFactory.getLogger(GlobalExceptionHandler.class);//2
      @ExceptionHandler(ArithmeticException.class)
      public ResponseResult doHandleArithmeticException(ArithmeticException e){
          e.printStackTrace();
          log.info("exception {}",e.getMessage());
          return new ResponseResult(e);// Encapsulate abnormal results 
      }
}

summary (Summary)

This section mainly explains Spring Boot How to standardize the response to logical data in engineering , Why is this design , The benefits of this design and how to design . Through the study of this section, we should improve the design concept of responding to the specification dimension , Master basic skills .

版权声明
本文为[Zhongkai]所创,转载请带上原文链接,感谢

Scroll to Top