1.建立统一返回类(根据需求添加需要返回的字段)
我这里使用了success,code,message,以及返回数据data,方法的生成可以直接使用lombok的注解
public class CommonResponse<T>{
private Boolean success;
private int code;
private String message;
private T data;
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public CommonResponse(){
}
//定义统一类型返回
public CommonResponse (ResultCode resultCode, Boolean success , T data){
this.success = success;
this.code = resultCode.getCode();
this.message = resultCode.getMessage();
this.data = data;
}
//因为重复使用成功与错误,所以建立了这两个,方便使用
public static<T> CommonResponse<T> ok(T data){
return new CommonResponse<>(ResultCode.HTTP_SUCCESS, true , data);
}
public static CommonResponse fail(){
return new CommonResponse<>(ResultCode.HTTP_FAIL, false, null);
}
}
响应体类
public enum ResultCode {
//各种响应根据需求去编写
HTTP_SUCCESS(200,”请求成功”),
HTTP_FAIL(500,”请求失败”),
private final Integer code;
private final String message;
public int getCode() {
return code;
}
public String getMessage() {
return message;
}
ResultCode(int code, String message) {
this.code = code;
this.message = message;
}
public Integer code(){
return this.code;
}
public String message(){
return this.message;
}
}
2.在Controller层使用
@PostMapping(value = “/addUser”)
public CommonResponse addUser(@RequestBody @Valid User user) {
System.out.println(user.getUsername());
return CommonResponse.ok(userService.findAll());
}
查看swagger返回结果
可以看到返回格式跟我们规定样式一样
3.全局配置返回
主要是实现ResponseBodyAdvice中的supports接口以及beforeBodyWrite,在返回前对数据进行处理supports为true是执行beforeBodyWrite
创建CommonResponseAdvice类
@RestControllerAdvice(basePackages = {“com.school.dcc.controller”})
public class CommonResponseAdvice implements ResponseBodyAdvice {
public static final Logger log = LoggerFactory.getLogger(CommonResponseAdvice.class);
@Override
public boolean supports(MethodParameter returnType, Class converterType) {
log.info(“进入全局组装统一返回========================”);
return !returnType.getGenericParameterType().equals(CommonResponse.class);
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
String returnClassType = returnType.getParameterType().getSimpleName();
log.info(“进入组装+++++++++++++++++++++++++++++++:” + returnClassType);
return CommonResponse.ok(body);
}
}
#### 这时的controller层,直接返回数据就好
“`java
@PostMapping(value = “/addUser”)
public List<User> addUser(@RequestBody @Valid User user) {
System.out.println(user.getUsername());
// 如果有参数校验失败,会将错误信息封装成对象组装在BindingResult里
return userService.findAll();
}
控制台返回
swagger返回
————————————————
版权声明:本文为CSDN博主「又菜又爱玩呜呜呜~」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/biancheng4/article/details/123024912