SpringBoot 通过注解封装API

包装前:

{"name":"王月","age":0}

包装后:

{"status":{"code":0,"name":"OK","msg":"succeed"},"result":{"name":"王月","age":0}}

实现思路:ResponseBodyAdvice+自定义注解

ResponseBodyAdvice实现类:
@ControllerAdvice
public class FocusResponseBodyAdvice implements ResponseBodyAdvice {

  /**
   * 在这个方法完成 "怎样封装结果集"
   */
  @Override
  public Object beforeBodyWrite(Object returnValue, MethodParameter methodParameter, MediaType mediaType, Class clas, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
    return ApiResp.ok(returnValue);
  }

  /**
   * 在这个方法完成 "是否对这个接口生效"  方案1.直接判断方法名 方案二2.判断哪些接口打注解[推荐]
   */

  @Override
  public boolean supports(MethodParameter methodParameter, Class clas) {
    AnnotatedElement annotatedElement = methodParameter.getAnnotatedElement();
    FocusAnnotation focusController = AnnotationUtils.findAnnotation(annotatedElement, FocusAnnotation.class);
    return focusController != null;
  }
}

自定义注解类:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FocusAnnotation {
}

基类:

public class GirlFriend {

  private String name;
  private int age;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

}

响应包装类:

  public class ApiResp {
  public static ApiResp OK = new ApiResp(Status.OK);

  public static ApiResp error(Status status) {
    return new ApiResp(status);
  }

  public static ApiResp ok(Object result) {
    return new ApiResp(Status.OK, result);
  }

  private final Status status;

  private final Object result;

  public ApiResp(Status status) {
    this(status, null);
  }

  public ApiResp(Status status, Object result) {
    this.status = status;
    this.result = result;
  }

  public Status getStatus() {
    return status;
  }

  public Object getResult() {
    return result;
  }
}

响应类包装类2:

public class Status {
  public static Status OK = new Status(0, "OK", "succeed");

  private final int code;
  private final String name;
  private String msg;

  /**
   * @param code 错误码
   */
  public Status(int code) {
    this(code, String.valueOf(code), String.valueOf(code));
  }

  /**
   * @param code 错误码
   * @param name 错误的名字
   */
  public Status(int code, String name) {
    this(code, name, name);
  }

  /**
   * @param code 错误码
   * @param name 错误的名字
   * @param msg  错误信息,用于提示
   */
  public Status(int code, String name, String msg) {
    this.code = code;
    this.name = name;
    this.msg = msg;
  }
  
  /**
   * @return 错误码
   */
  public int getCode() {
    return code;
  }

  /**
   * @return 错误的名字
   */
  public String getName() {
    return name;
  }

  /**
   * @return 错误信息,用于提示
   */
  public String getMsg() {
    return msg;
  }

  /**
   * @param msg 错误信息,用于提示
   */
  public Status setMsg(String msg) {
    this.msg = msg;
    return this;
  }

}

Controller类:

@Controller
public class MainController {

    @RequestMapping(value = "/annotation",method = RequestMethod.GET)
    @ResponseBody
    @FocusAnnotation   //只有第一个加了自定义注解
    public GirlFriend sayHello(){
        GirlFriend girlFriend = new GirlFriend();
        girlFriend.setName("王月");
        return girlFriend;
    }

    @RequestMapping(value = "/annotation2",method = RequestMethod.GET)
    @ResponseBody
    public GirlFriend sayHello2(){
        GirlFriend girlFriend = new GirlFriend();
        girlFriend.setName("王月");
        return girlFriend;
    }
}

 

你可能感兴趣的:(后端)