spring boot 給返回值加狀態 BaseData

JavaWeb開發中,須要給前端返回的數據加上一些頭部的狀態信息,來表示請求成功或失敗的狀態緣由前端

1、數據添加狀態信息web

  1.新建BaseDataspring

public class BaseData<T> {

    private String returnCode;
    private String returnMsg;
    private T data;
    //其餘的get set toString 方法略
}

  2.定義工具類:json

public final class ReturnInfoUtils {
    private ReturnInfoUtils(){}
    //返回狀態信息
    public static final String SUCCESSCODE="100";
    public static final String SUCCESSMSG="操做成功";
    public static final String FAILCODE="500";
    public static final String FAILMSG="操做失敗";
}

  3.定義註解:mvc

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReturnWrapper {

    String returnCode() default ReturnInfoUtils.SUCCESSCODE;
    String returnMsg() default  ReturnInfoUtils.SUCCESSMSG;
}

  4.新建類繼承AbstractMappingJacksonResponseBodyAdvice並重寫一個切面方法beforeBodyWriteInternalapp

  

 1 package com.zpark.tools.interceptor;
 2 
 3 import com.zpark.response.ReturnDto;
 4 import org.springframework.core.MethodParameter;
 5 import org.springframework.http.MediaType;
 6 import org.springframework.http.converter.json.MappingJacksonValue;
 7 import org.springframework.http.server.ServerHttpRequest;
 8 import org.springframework.http.server.ServerHttpResponse;
 9 import org.springframework.web.bind.annotation.RestControllerAdvice;
10 import org.springframework.web.servlet.mvc.method.annotation.AbstractMappingJacksonResponseBodyAdvice;
11 
12 /**
13  * @author cosmo
14  * @Title: CommonWrsControllerAdvice
15  * @ProjectName
16  * @Description: TODO
17  * @date  
18  */
19 @RestControllerAdvice
20 public class CommonWrsControllerAdvice extends AbstractMappingJacksonResponseBodyAdvice {
21     @Override
22     protected void beforeBodyWriteInternal(MappingJacksonValue mappingJacksonValue, MediaType mediaType, MethodParameter methodParameter, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
23         if (methodParameter.getMethod().isAnnotationPresent(RerutnWrapper.class)) {
24             mappingJacksonValue.setValue(getWrapperResponse(methodParameter,serverHttpRequest, mappingJacksonValue.getValue()));
25         }
26     }
27 
28     private ReturnDto getWrapperResponse(MethodParameter returnType, ServerHttpRequest req, Object data) {
29         ReturnWrapper wrapper=returnType.getMethod().getAnnotation(ReturnWrapper.class);
31         BaseData = new BaseData();
32         returnDto.setData(data);
33         returnDto.setreturnCode(wrapper.ReturnCode());
34         return returnDto;
35     }
37 }

就能夠用註解來返回的數據ide

 1   /**
 2      * 測試.
 3      *
 4      * @return the attachment info
 5      */
 6     @ResponseBody
 7     @RequestMapping(value = {"test"})
 8     @RestWrapper(msg = ReturnInfoUtils.GETSUCCESS)
 9     public List<Employee> test() {
10         List<Employee> employeeList=new ArrayList<>();
11         Employee employee=new Employee();
12         employee.setName("lisi");
13         employeeList.add(employee);
14         return employeeList;
15     }
16  

返回數據:工具

{
    "returnCode": "SUCCESS",
    "returnMsg": "操做成功",
    "data": [
        {
            "name": "lisi"
        }
    ]
}
相關文章
相關標籤/搜索