1、引入spring-boot-starter-web依赖和new-swagger依赖
<dependency>
<groupId>com.jjw</groupId>
<artifactId>new-swagger</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、创建一个springboot的模块,其目录结构如下图:
3、添加能被spring扫描到的全局异常处理类的控制器增强注解在 GlobalExceptionHandler中,其内容如下:
package com.jjw.result.exception;
import com.jjw.result.pojo.Result;
import com.jjw.result.pojo.StatusCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
//写一个方法 方法 用于当controller发送异常的时候被调用 要捕获异常 代替 controller 去返回给前端
//系统异常
@ExceptionHandler(value=Exception.class)
public Result handlerException(Exception e){
// e.printStackTrace();
logger.error("出问题了", e);
return Result.error();
}
// @ExceptionHandler 异常处理器 与上面注解一起使用,可以拦截指定的异常信息并做相关的处理
//LeadNewsException业务上的异常 才进行处理
@ExceptionHandler(value=LeadNewsException.class)
public Result handlerLeadNewsException(LeadNewsException e){
// e.printStackTrace();
logger.error("出大问题了", e);
return Result.errorMessage(e.getMessage(), StatusCode.CUSTOM_FAILURE.code(),null);
}
}
4、自定义异常类LeadNewsException的内容为:
package com.jjw.result.exception;
import com.jjw.result.pojo.StatusCode;
/**
* 自定义异常类 用于业务上的异常(比如商品找不到 404)
*/
public class LeadNewsException extends Exception{
//错误的状态码
private Integer code;
//错误信息
private String message;
public LeadNewsException(){
}
public LeadNewsException(Integer code, String message){
this.code=code;
this.message=message;
}
public LeadNewsException(String message){
//自定义的异常的状态
this.code= StatusCode.CUSTOM_FAILURE.code();
this.message=message;
}
@Override
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
}
3、PageInfo的内容:
package com.jjw.result.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "分页对象")
public class PageInfo<T> implements Serializable {
//当前页码
@ApiModelProperty(notes = "当前页码")
private Long page;
//每页显示行
@ApiModelProperty(notes = "每页显示的行")
private Long size;
//总记录数
@ApiModelProperty(notes = "总记录数")
private Long total;
//总页数
@ApiModelProperty(notes = "总页数")
private Long totalPages;
//当前页记录
@ApiModelProperty(notes = "当前页记录")
private List<T> list;
}
PageRequestDto的内容
package com.jjw.result.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Data
@ApiModel(description = "分页请求对象")
public class PageRequestDto<T> implements Serializable {
//当前页码
@ApiModelProperty(notes = "当前页码")
private Long page = 1L;
//每页显示的行
@ApiModelProperty(notes = "每页显示的行")
private Long size = 10L;
//请求体实体对象
@ApiModelProperty(notes = "请求体条件对象")
private T body;
}
Result的内容
package com.jjw.result.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel(description = "通用返回响应结果对象", value = "result")
public class Result<T> implements Serializable {
@ApiModelProperty(notes = "响应结果的错误信息", required = true)
private String message;
@ApiModelProperty(notes = "响应结果的错误信息状态码,2000X表示成功,4000X表示错误,5000X表示服务端异常", required = true)
private Integer code;
@ApiModelProperty(notes = "响应结果逻辑数据", required = true)
private T data;
//成功 并不返回数据
public static <T> Result<T> ok() {
return new Result<T>(StatusCode.SUCCESS.message(), StatusCode.SUCCESS.code(), null);
}
//成功 并返回数据
public static <T> Result<T> ok(T data) {
return new Result<T>(StatusCode.SUCCESS.message(), StatusCode.SUCCESS.code(), data);
}
//系统错误 不返回数据
public static <T> Result<T> error() {
return new Result<T>(StatusCode.FAILURE.message(), StatusCode.FAILURE.code(), null);
}
//系统错误 并返回逻辑数据
public static <T> Result<T> error(T data) {
return new Result<T>(StatusCode.FAILURE.message(), StatusCode.FAILURE.code(), data);
}
//错误并返回指定错误信息和状态码以及逻辑数据
public static <T> Result<T> error(StatusCode statusCode, T data) {
return new Result<T>(statusCode.message(), statusCode.code(), data);
}
// 错误并返回指定错误信息和状态码 不返回数据
public static <T> Result<T> error(StatusCode statusCode) {
return new Result<T>(statusCode.message(), statusCode.code(), null);
}
//自定义错误和状态返回
public static <T> Result<T> errorMessage(String message, Integer code, T data) {
return new Result<T>(message, code, data);
}
public Result() {
}
public Result(String message, Integer code, T data) {
this.message = message;
this.code = code;
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
StatusCode的内容
package com.jjw.result.pojo;
public enum StatusCode {
//区分与http状态码
SUCCESS(20000, "操作成功"),
PARAM_ERROR(40000, "参数异常"),
NOT_FOUND(40004, "资源不存在"),
FAILURE(50000, "系统异常"),
NEED_LOGIN(50002, "需要登录"),
CUSTOM_FAILURE(50001, "自定义异常错误");
private final Integer code;
private final String message;
StatusCode(Integer code, String message) {
this.code = code;
this.message = message;
}
//获取状态码
public Integer code() {
return code;
}
//获取信息
public String message() {
return message;
}
@Override
public String toString() {
return String.valueOf(this.code);
}
}