目录
1.父工程pom
<properties>
<!-- Spring Cloud Hoxton.SR8 依赖 -->
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<!-- spring cloud alibaba 依赖 -->
<spring-cloud-alibaba.version>2.2.4.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<!-- lombok 工具通过在代码编译时期动态的将注解替换为具体的代码,
IDEA 需要添加 lombok 插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<!--工具类集合-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.0</version>
</dependency>
<!-- 引入jwt-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.10.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.10.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.10.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>
<!-- 项目依赖管理 父项目只是声明依赖,子项目需要写明需要的依赖(可以省略版本信息) -->
<dependencyManagement>
<dependencies>
<!-- spring cloud 依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- spring cloud alibaba 依赖 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 配置远程仓库 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2.全局异常捕获pom依赖
<!-- 引入 Web 功能 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.注解类
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 忽略统一相应注解定义:使用该注解的类和方法忽略统一响应
*/
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreResponseAdvice {
}
4.定义统一响应拦截
import xx.xx.IgnoreResponseAdvice;
import xx.xx.vo.CommonResponse;
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.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
/**
* 实现统一响应
*/
@RestControllerAdvice(value = {"xx.xxx"})
public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> {
/**
* 判断是否需要对相应处理
* @param methodParameter
* @param aClass
* @return
*/
@Override
@SuppressWarnings("all")
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
//判断是否需要对响应数据进行处理
//类
if(methodParameter.getDeclaringClass().isAnnotationPresent(IgnoreResponseAdvice.class)){
return false;
}
//方法
if(methodParameter.getMethod().isAnnotationPresent(IgnoreResponseAdvice.class)){
return false;
}
//为true beforeBodyWrite
return true;
}
/**
*
* @param o 响应对象
* @param methodParameter
* @param mediaType
* @param aClass
* @param serverHttpRequest
* @param serverHttpResponse
* @return
*/
@Override
public Object beforeBodyWrite(Object o,
MethodParameter methodParameter,
MediaType mediaType,
Class<? extends HttpMessageConverter<?>> aClass,
ServerHttpRequest serverHttpRequest,
ServerHttpResponse serverHttpResponse) {
//定义最终返回对象
CommonResponse<Object> response = new CommonResponse<>(200,"");
if(null == o){
return response;
}else if(o instanceof CommonResponse){
response=(CommonResponse<Object>) o;
return response;
}else{
//将o设置为返回对象中的o
response.setData(o);
}
return response;
}
}
5.统一响应类
package xx.xx.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 通用相应对象定义
* {
* "code":200,
* "msg":"",
* "data":{}
* }
*/
@Data
//无参构造函数
@NoArgsConstructor
//有参构造函数
@AllArgsConstructor
public class CommonResponse<T> implements Serializable {
/**
* 错误码
*/
private Integer code;
/**
* 错误信息
*/
private String msg;
/**
* 泛型响应数据
*/
private T data;
public CommonResponse(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
}
6.全局异常捕获类
package xx.xx.advice;
import xx.xx.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
/**
* 全局异常捕获处理
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionAdvice {
/**
* Exception.class 捕获的class类型
* @param req
* @param ex
* @return
*/
@ExceptionHandler(value = Exception.class)
public CommonResponse<String> handlerCommerceException(HttpServletRequest req,
Exception ex) {
CommonResponse<String> response = new CommonResponse<>(-1,"business error");
response.setData(ex.getMessage());
log.error("commerce service has error:[{}]",ex.getMessage(),ex);
return response;
}
}
7.使用
在web项目中引入该依赖即可,注意包名 <dependency> <groupId>XX.xx</groupId> <artifactId>cloud-mvc-config</artifactId> <version>1.0-SNAPSHOT</version> </dependency>