先感慨一下:要改掉不做笔记的坏毛病呀! 先提一嘴:spring boot 配置 Filter 有两种方式,一种是编程方式(spring
boot 最开始支持的方式), 另一种是注解方式(spring boot 后来又加了的一种配置方式)。 这几个月加班实在厉害,
看了点东西,却没有做笔记, 后续会补上, 并且后面会一直做笔记了,也希望看到这篇博客的搬砖新老同志们也做做笔记啊!
之前在一个自己的练手项目中使用第一种配置方式配置过, 但是没有做笔记,今天又要配置了, 想了想看看以前的吧, 找了好久,就是没找到,
(不对呀!肯定配过, 怎么就没了呢!啊!!!) 言归正传
统一返回数据格式
统一返回数据格式, 顾名思义, 同一个项目中所有返回的数据格式相同, 使前端(iOS Android, Web)对数据的操作更轻松一些, 前台需要拿到后台返回到前台的状态信息, 来响应的做处理, 如通讯异常, 操作成功, 存储失败等!
统一返回数据格式也没有一个具体的格式, 只要能描述清楚返回数据状态以及要返回的具体数据就可以:
/**
- 统一返回实体
- - @author zhangchenzhao
- @create 2018-08-08 下午11:25
*/
public class ResponseData {
/**
* @Author : Zcz
* @Description: 状态码
* @Date: 2018/8/19 - 下午2:48
*/
private String code;
/**
* @Author : Zcz
* @Description: 是否成功
* @Date: 2018/8/19 - 下午2:48
*/
private Boolean status;
/**
* @Author : Zcz
* @Description: 状态信息
* @Date: 2018/8/19 - 下午2:48
*/
private String msg; //
/**
* @Author : Zcz
* @Description: :返回数据
* @Date: 2018/8/19 - 下午2:47
*/
public T data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public ResponseData(T data) {
this.code = "000000";
this.msg = "成功";
this.status = true;
this.data = data;
}
public ResponseData() {
this.code = "000000";
this.msg = "成功";
this.status = true;
}
public ResponseData(String code, Boolean status, String msg, T data) {
this.code = code;
this.status = status;
this.msg = msg;
this.data = data;
}
public ResponseData(ResponseEnum responseEnum, T data){
this.code = responseEnum.getCode();
this.status = responseEnum.getStatus();
this.msg = responseEnum.getMsg();
this.data = data;
}
public ResponseData(Exception e){
this.code = "000012";
this.status = false;
this.msg = e.getMessage();
}
@Override
public String toString() {
return "ResponseData{" +
"code='" + code + '\'' +
", status=" + status +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
/**
* 返回信息枚举
* * @author zhangchenzhao
* @create 2018-08-08 下午11:42
*/
public enum ResponseEnum {
SUCCESS("000000", true, "成功"),
SAVE_ERROR("000001", false, "保存数据失败"),
DELETE_ERROR("000002", false, "删除数据失败"),
UPDATE_ERROR("000003", false, "更新数据失败"),
SELECT_ERROR("000004", false, "查询数据失败");
private String code;
private Boolean status;
private String msg;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
ResponseEnum(String code, Boolean status, String msg) {
this.code = code;
this.status = status;
this.msg = msg;
}
}
import com.bbuilder.personal.config.filter.UnifyExceptionFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
/**
* @author zhangchenzhao
*/
@SpringBootApplication
public class PersonalApplication {
public static void main(String[] args) {
SpringApplication.run(PersonalApplication.class, args);
}
@Bean
public FilterRegistrationBean exceptionFilter(){
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new UnifyExceptionFilter());
registration.addUrlPatterns("/*");
// registration.addInitParameter("paramName", "paramValue");
registration.setName("exceptionFilter");
registration.setOrder(1);
return registration;
}
}
/**
* @author zhangchenzhao
*/
@SpringBootApplication
@ServletComponentScan
public class PersonalApplication {
public static void main(String[] args) {
SpringApplication.run(PersonalApplication.class, args);
}
}
/**
* 统一异常处理过滤器
*
* @author zhangchenzhao
* @create 2018-08-19 下午3:23
*/
@WebFilter(filterName = "exceptionFilter", urlPatterns = "/*")
@Order(1)
public class UnifyExceptionFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
try {
filterChain.doFilter(servletRequest ,servletResponse);
}catch (BaseException e){
exceptionDataHandel((HttpServletResponse)servletResponse, e);
}catch (Exception e){
exceptionDataHandel((HttpServletResponse)servletResponse, (BaseException) e);
}catch (Error error){
System.out.println(error.getCause());
}
}
/**
* @Author : Zcz
* @Description: 整理异常数据
* @Date: 2018/8/20 - 上午12:22
*/
public void exceptionDataHandel(HttpServletResponse response, BaseException e)
throws ServletException, IOException {
String msg = e.getMessage();
if(e.getObjects()!=null && e.getObjects().length != 0){
msg = msg+": [";
for (Object obj :
e.getObjects()) {
msg = msg + obj.toString()+",";
}
msg = msg .substring(0, msg.length()-1);
}
responseExceptionMsg(response, msg);
}
/**
* @Author : Zcz
* @Description: 返回异常时前端需要的数据
* @Date: 2018/8/20 - 上午12:22
*/
public void responseExceptionMsg(HttpServletResponse response, String msg)
throws ServletException, IOException {
response.getWriter().write(msg);
}
}
注:@webFilter 注解是servlet3.0的原生注解,并不是spring boot的注解, 但这种注册方式仍然是与代码配置方式相同
参考: https://blog.csdn.net/king_is_everyone/article/details/53116744