spring boot 给返回值加状态 BaseData详解编程语言

JavaWeb开发中,需要给前端返回的数据加上一些头部的状态信息,来表示请求成功或失败的状态原因

一、数据添加状态信息

  1.新建BaseData

public class BaseData<T> { 
 
    private String returnCode; 
    private String returnMsg; 
    private T data; 
    //其他的get set toString 方法略 
}

  2.定义工具类:

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.定义注解:

@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface ReturnWrapper { 
 
    String returnCode() default ReturnInfoUtils.SUCCESSCODE; 
    String returnMsg() default  ReturnInfoUtils.SUCCESSMSG; 
}

  4.新建类继承AbstractMappingJacksonResponseBodyAdvice并重写一个切面方法beforeBodyWriteInternal

  

 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 }

就可以用注解来返回的数据

 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" 
        } 
    ] 
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/18576.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论