1. 需求
-
在使用springboot的使用,我们更加多的方式是返回json数据,直接返回,如下(比如返回一个对象): {
"username":"小明",
"sex":"男"
}
-
如上例子,是正常的情况下获取的,那如果不正常的情况下,则会抛异常。 -
而如今,调用方A调用系统B的时候,系统B出现错误,无法正常返回(如果不特殊处理)json数据,而我调用方A又只想接收json数据,即使报错了,也很想知道到底调用成功与否,能不能统一一下返回值,有什么标志告诉我调用是否成功呢? -
因此,实际上对于系统的返回值,我们可以双方做一些统一,如下: {
"code":"",
"message":"",
"data":""
}
2. 统一返回值快速入门(代码)
-
按照第一大点所说,我们协商定,统一返回值是以如下格式: {
"code":"",
"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> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@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);
}
}
}
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;
@RestControllerAdvice
public class ResponseBodyAdviceTest implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return Objects.equals("test", returnType.getMethod().getName());
}
@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> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return Objects.equals("test", returnType.getMember().getName());
}
@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);
}
}
}
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> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return Objects.equals("test", returnType.getMember().getName());
}
@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对异常的统一处理方法可以参考:https://blog.csdn.net/xueyijin/article/details/122527688 优化代码: @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> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@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)时候的特殊处理
下次再补啦,睡觉觉,明天上班
|