文章目录
- 1. 需求
- 2. 统一返回值快速入门(代码)
- 3. ResponseBodyAdvice接口的细节
- 0. 实现该接口的类必须要加上@ControllerAdvice或者@RestControllerAdvice controller的切面。
- 1. ResponseBodyAdvice的supports方法使用
- 2. 对于String类型的返回值需要特殊处理
- 3. 对于出现异常的返回值统一封装注意事项
- 4. 针对访问路径不存在(404)时候的特殊处理
1. 需求
- 在使用springboot的使用,我们更加多的方式是返回json数据,直接返回,如下(比如返回一个对象):
{
"username":"小明",
"sex":"男"
}
- 如上例子,是正常的情况下获取的,那如果不正常的情况下,则会抛异常。
- 而如今,调用方A调用系统B的时候,系统B出现错误,无法正常返回(如果不特殊处理)json数据,而我调用方A又只想接收json数据,即使报错了,也很想知道到底调用成功与否,能不能统一一下返回值,有什么标志告诉我调用是否成功呢?
- 因此,实际上对于系统的返回值,我们可以双方做一些统一,如下:
{
"code":"", // 若code 为success则表示调用成功,若不为success说明调用不成功
"message":"",// 这是专门为了报错的时候,存放报错信息
"data":"" // 这是专门存放 正常情况下的返回值
}
2. 统一返回值快速入门(代码)
- 按照第一大点所说,我们协商定,统一返回值是以如下格式:
{
"code":"", // 若code 为success则表示调用成功,若不为success说明调用不成功
"message":"",// 这是专门为了报错的时候,存放报错信息
"data":"" // 这是专门存放 正常情况下的返回值
}
- 代码:
@RestController
public class TestController {
@GetMapping("/test")
public User test() {
return new User("小明","男");
}
}
@Data
@AllArgsConstructor
public class User {
private String username;
private String sex;
}
@RestControllerAdvice
public class ResponseBodyAdviceTest implements ResponseBodyAdvice<Object> {
//判断是否要执行beforeBodyWrite方法,true为执行,false不执行
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
//对response处理的执行方法
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
// 这里面参数很多,一般使用如下几个:
// body 返回的内容 request 请求 response 响应
return Response.createResponse(body);
}
@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public static class Response<T> {
private final String code;
private String message;
private T data;
private Response() {
this.code = "success";
}
private Response(T data) {
this();
this.data = data;
}
public static <T> Response<T> createResponse(T data) {
return new Response<>(data);
}
}
}
3. ResponseBodyAdvice接口的细节
0. 实现该接口的类必须要加上@ControllerAdvice或者@RestControllerAdvice controller的切面。
1. ResponseBodyAdvice的supports方法使用
- 代码:
@RestController
public class TestController {
@GetMapping("/test")
public User test() {
return new User("小明","男");
}
@GetMapping("/test2")
public User test2() {
return new User("小红","男");
}
}
package com.king.learning;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import java.util.Objects;
/**
* @author JIN
* @description
* @createTime 2022-01-16 21:00
**/
@RestControllerAdvice
public class ResponseBodyAdviceTest implements ResponseBodyAdvice<Object> {
//判断是否要执行beforeBodyWrite方法,true为执行,false不执行
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
// 如果方法名字是test则返回true,则执行下面的beforeBodyWrite方法
// 如何方法名字不是test,则不执行下面的beforeBodyWrite方法
return Objects.equals("test", returnType.getMethod().getName());
}
//对response处理的执行方法
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
return Response.createResponse(body);
}
@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public static class Response<T> {
private final String code;
private String message;
private T data;
private Response() {
this.code = "success";
}
private Response(T data) {
this();
this.data = data;
}
public static <T> Response<T> createResponse(T data) {
return new Response<>(data);
}
}
}
2. 对于String类型的返回值需要特殊处理
- 我们知道spring对于controller层返回值是String类型的时候,是使用了StringHttpMessageConverter转换器,无法转换为Json格式。
- 代码例子验证:
@RestController
public class TestController {
@GetMapping("/test")
public String test() {
return "hello world";
}
@GetMapping("/test2")
public String test2() {
return "hello world";
}
@GetMapping("/test3")
public int test3() {
return 3;
}
}
由上两个图片,可以看出,String类型的返回值确实没有转json类型。
- 因此,在使用封装统一返回值的时候,如果出现String类型的返回值body的时候,且没有特殊处理(即手动转json),则会报错如下:
- 因此,再处理返回值的时候,要判如果是String类型,则手动转json,如下:
@RestControllerAdvice
public class ResponseBodyAdviceTest implements ResponseBodyAdvice<Object> {
//判断是否要执行beforeBodyWrite方法,true为执行,false不执行
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
// return Objects.equals("test", returnType.getMember().getName());
return Objects.equals("test", returnType.getMember().getName());
}
//对response处理的执行方法
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
// 判是否为String
if (body instanceof String) {
// 手动转json 并且返回String,这样子spring不会再处理,直接返回的String即就是json数据了
return toJson(Response.createResponse(body));
}
return Response.createResponse(body);
}
private Object toJson(Response response) {
try {
return new ObjectMapper().writeValueAsString(response);
} catch (JsonProcessingException e) {
throw new RuntimeException("无法转发json格式", e);
}
}
@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public static class Response<T> {
private final String code;
private String message;
private T data;
private Response() {
this.code = "success";
}
private Response(T data) {
this();
this.data = data;
}
public static <T> Response<T> createResponse(T data) {
return new Response<>(data);
}
}
}
3. 对于出现异常的返回值统一封装注意事项
- 如下:在出现错误,则会产生RuntimeException异常,并抛出。
@RestController
public class TestController {
@GetMapping("/test")
public String test() {
int i = 1/0;
return "hello world";
}
}
@RestControllerAdvice
public class ResponseBodyAdviceTest implements ResponseBodyAdvice<Object> {
//判断是否要执行beforeBodyWrite方法,true为执行,false不执行
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
// return Objects.equals("test", returnType.getMember().getName());
return Objects.equals("test", returnType.getMember().getName());
}
//对response处理的执行方法
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body instanceof String) {
return toJson(Response.createResponse(body));
}
return Response.createResponse(body);
}
private Object toJson(Response response) {
try {
return new ObjectMapper().writeValueAsString(response);
} catch (JsonProcessingException e) {
throw new RuntimeException("无法转发json格式", e);
}
}
@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public static class Response<T> {
private final String code;
private String message;
private T data;
private Response() {
this.code = "success";
}
private Response(T data) {
this();
this.data = data;
}
public static <T> Response<T> createResponse(T data) {
return new Response<>(data);
}
}
}
完全没有封装统一返回值,为什么呢?
因为出现错误的时候,是抛出一个异常,抛出异常,然后到RestControllerAdvice,而又没有对异常进行捕捉什么操作,自然继续抛异常,压根就不会进行执行返回值处理方法。
- 一种解决方法(不太建议)
在controller层直接try catch 如果有异常,直接返回e
@RestController
public class TestController {
@GetMapping("/test")
public Object test() {
try {
int i = 1/0;
return "hello world";
} catch (Exception e){
return e;
}
}
}
虽然这样子可以封装数据,但是controller层的代码变得太啰嗦了,因此我们需要使用spring对异常的统一处理。
- 推荐方式(结合spring对异常的统一处理方法)
spring对异常的统一处理方法可以参考:
优化代码:
@RestController
public class TestController {
@GetMapping("/test")
public String test() {
int i = 1 / 0;
return "hello world";
}
}
@RestControllerAdvice
public class MyException {
@ExceptionHandler(Exception.class)
public Throwable handleException(Exception e) {
return e;
}
}
@RestControllerAdvice
public class ResponseBodyAdviceTest implements ResponseBodyAdvice<Object> {
//判断是否要执行beforeBodyWrite方法,true为执行,false不执行
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
// 这里默认都执行下面beforeBodyWrite 方法,因为我们是作整个系统统一返回值的处理
// 反正String问题,异常问题都处理了
return true;
}
//对response处理的执行方法
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body instanceof Throwable) {
return handleException((Throwable) body);
} else if (body instanceof String) {
return toJson(Response.createResponse(body));
}
return Response.createResponse(body);
}
// 针对异常的时候,统一返回值的处理
private Response<?> handleException(Throwable throwable) {
return Response.builder()
.code("failed")
.message(throwable.getMessage())
.build();
}
private Object toJson(Response<?> response) {
try {
return new ObjectMapper().writeValueAsString(response);
} catch (JsonProcessingException e) {
throw new RuntimeException("无法转发json格式", e);
}
}
@Data
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@AllArgsConstructor
public static class Response<T> {
private final String code;
private String message;
private T data;
private Response() {
this.code = "success";
}
private Response(T data) {
this();
this.data = data;
}
public static <T> Response<T> createResponse(T data) {
return new Response<>(data);
}
}
}
4. 针对访问路径不存在(404)时候的特殊处理
下次再补啦,睡觉觉,明天上班