springboot统一restful返回值及统一异常处理

统一返回restful数据:

首先确定需要返回的json数据的格式,定义一个统一返回类

package com.unicom.microservice.peixun.peixun_matb3.Response;

import com.unicom.microservice.peixun.peixun_matb3.enums.UnicomResponseEnums;

/**

* @program: 测试

* @description:返回的JSON数据结构标准

* @author: mtb

* @create: 2018-10-17 09:01

**/

public class ResponseBean <T>{

private boolean success;

private T data;

private String errCode;

private String errMsg;

public ResponseBean(){}

public ResponseBean(boolean success, T data) {

super();

this.success = success;

this.data = data;

}

@Override

public String toString() {

return "ResponseBean{" +

"success=" + success +

", data=" + data +

", errCode='" + errCode + '\'' +

", errMsg='" + errMsg + '\'' +

'}';

}

public ResponseBean(boolean success, T data, String errCode, String errMsg) {

super();

this.success = success;

this.data = data;

this.errCode = errCode;

this.errMsg = errMsg;

}

public ResponseBean(boolean success, String errCode, String errMsg) {

this.success = success;

this.errCode = errCode;

this.errMsg = errMsg;

}

public ResponseBean(boolean success,UnicomResponseEnums enums){

this.success=success;

this.errCode=enums.getCode();

this.errMsg=enums.getMsg();

}

public ResponseBean(boolean success,T data,UnicomResponseEnums enums){

this.success=success;

this.data=data;

this.errCode=enums.getCode();

this.errMsg=enums.getMsg();

}

public boolean isSuccess() {

return success;

}

public void setSuccess(boolean success) {

this.success = success;

}

public T getData() {

return data;

}

public void setData(T data) {

this.data = data;

}

public String getErrCode() {

return errCode;

}

public void setErrCode(String errCode) {

this.errCode = errCode;

}

public String getErrMsg() {

return errMsg;

}

public void setErrMsg(String errMsg) {

this.errMsg = errMsg;

}

}

新建一个枚举类,将常用提示信息及错误信息写入:

package com.unicom.microservice.peixun.peixun_matb3.enums;

/**

* @program: 测试

* @description:友好提示枚举

* @author: mtb

* @create: 2018-10-17 08:52

**/

public enum UnicomResponseEnums {

SYSTEM_ERROR("-001","系统异常"),

BAD_REQUEST("-002","错误的请求参数"),

NOT_FOUND("-003","找不到请求路径!"),

CONNECTION_ERROR("-004","网络连接请求失败!"),

METHOD_NOT_ALLOWED("-005","不合法的请求方式"),

DATABASE_ERROR("-004","数据库异常"),

BOUND_STATEMENT_NOT_FOUNT("-006","找不到方法!"),

REPEAT_REGISTER("001","重复注册"),

NO_USER_EXIST("002","用户不存在"),

INVALID_PASSWORD("003","密码错误"),

NO_PERMISSION("004","非法请求!"),

SUCCESS_OPTION("005","操作成功!"),

NOT_MATCH("-007","用户名和密码不匹配"),

FAIL_GETDATA("-008","获取信息失败"),

BAD_REQUEST_TYPE("-009","错误的请求类型"),

INVALID_MOBILE("010","无效的手机号码"),

INVALID_EMAIL("011","无效的邮箱"),

INVALID_GENDER("012","无效的性别"),

REPEAT_MOBILE("014","已存在此手机号"),

REPEAT_EMAIL("015","已存在此邮箱地址"),

NO_RECORD("016","没有查到相关记录"),

LOGIN_SUCCESS("017","登陆成功"),

LOGOUT_SUCCESS("018","已退出登录"),

SENDEMAIL_SUCCESS("019","邮件已发送,请注意查收"),

EDITPWD_SUCCESS("020","修改密码成功"),

No_FileSELECT("021","未选择文件"),

FILEUPLOAD_SUCCESS("022","上传成功"),

NOLOGIN("023","未登陆"),

ILLEGAL_ARGUMENT("024","参数不合法"),

ERROR_IDCODE("025","验证码不正确");

private String code;

private String msg;

private UnicomResponseEnums(String code, String msg) {

this.code = code;

this.msg = msg;

}

public String getCode() {

return code;

}

public void setCode(String code) {

this.code = code;

}

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

}

建立一个自定义异常继承 RuntimeException类,以便抛出自定义异常

package com.unicom.microservice.peixun.peixun_matb3.exception;

import com.unicom.microservice.peixun.peixun_matb3.enums.UnicomResponseEnums;

/**

* @program: 测试

* @description:自定义异常

* @author: mtb

* @create: 2018-10-17 08:57

**/

public class UnicomRuntimeException extends RuntimeException {

private static final long serialVersionUID = 1L;

protected String code;

protected String msg;

protected String message;//打印出的日志信息

public UnicomRuntimeException(UnicomResponseEnums enums, String message) {

super();

this.code = enums.getCode();

this.msg = enums.getMsg();

this.message = message;

}

public UnicomRuntimeException(UnicomResponseEnums enums) {

super();

this.code = enums.getCode();

this.msg = enums.getMsg();

}

public String getCode() {

return code;

}

public void setCode(String code) {

this.code = code;

}

public String getMsg() {

return msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

public String getMessage() {

return message;

}

public void setMessage(String message) {

this.message = message;

}

public UnicomRuntimeException() {

super();

}

public UnicomRuntimeException(String message, Throwable cause) {

super(message, cause);

}

public UnicomRuntimeException(String message) {

super(message);

}

}

新建一个异常处理类:

package com.unicom.microservice.peixun.peixun_matb3.exception;

import javax.servlet.http.HttpServletRequest;

import com.unicom.microservice.peixun.peixun_matb3.Response.ResponseBean;

import com.unicom.microservice.peixun.peixun_matb3.enums.UnicomResponseEnums;

import org.apache.ibatis.binding.BindingException;

import org.omg.CosNaming.NamingContextPackage.NotFound;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.dao.DataAccessException;

import org.springframework.http.HttpStatus;

import org.springframework.stereotype.Controller;

import org.springframework.validation.BindException;

import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.NoHandlerFoundException;

import java.net.ConnectException;

import java.sql.SQLException;

/**

* @program: 测试

* @description:全局的异常处理类

* @author: mtb

* @create: 2018-10-19 11:38

**/

@RestControllerAdvice(annotations={RestController.class,Controller.class})

public class SpringExceptionHandle {

private static final Logger logger = LoggerFactory.getLogger(SpringExceptionHandle.class);

/**

* 请求参数类型错误异常的捕获

* @param e

* @return

*/

@ExceptionHandler(value={BindException.class})

@ResponseBody

@ResponseStatus(value=HttpStatus.BAD_REQUEST)

public ResponseBean<String> badRequest(BindException e){

logger.error("occurs error when execute method ,message {}",e.getMessage());

return new ResponseBean<>(false, UnicomResponseEnums.BAD_REQUEST);

}

/**

* 404错误异常的捕获

* @param e

* @return

*/

@ExceptionHandler(value={NoHandlerFoundException.class})

@ResponseBody

@ResponseStatus(value=HttpStatus.NOT_FOUND)

public ResponseBean<String> badRequestNotFound(BindException e){

logger.error("occurs error when execute method ,message {}",e.getMessage());

return new ResponseBean<>(false,null, UnicomResponseEnums.NOT_FOUND);

}

/**

* mybatis未绑定异常

* @param e

* @return

*/

@ExceptionHandler(BindingException.class)

@ResponseBody

@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)

public ResponseBean<String> mybatis(Exception e){

logger.error("occurs error when execute method ,message {}",e.getMessage());

return new ResponseBean<>(false,UnicomResponseEnums.BOUND_STATEMENT_NOT_FOUNT);

}

/**

* 自定义异常的捕获

* 自定义抛出异常。统一的在这里捕获返回JSON格式的友好提示。

* @param exception

* @param request

* @return

*/

@ExceptionHandler(value={UnicomRuntimeException.class})

@ResponseBody

@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)

public <T> ResponseBean<T> sendError(UnicomRuntimeException exception,HttpServletRequest request){

String requestURI = request.getRequestURI();

logger.error("occurs error when execute url ={} ,message {}",requestURI,exception.getMsg());

return new ResponseBean<>(false,exception.getCode(),exception.getMsg());

}

/**

* 数据库操作出现异常

* @param e

* @return

*/

@ExceptionHandler(value={SQLException.class,DataAccessException.class})

@ResponseBody

@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)

public ResponseBean<String> systemError(Exception e){

logger.error("occurs error when execute method ,message {}",e.getMessage());

return new ResponseBean<>(false, UnicomResponseEnums.DATABASE_ERROR);

}

/**

* 网络连接失败!

* @param e

* @return

*/

@ExceptionHandler(value={ConnectException.class})

@ResponseBody

@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)

public ResponseBean<String> connect(Exception e){

logger.error("occurs error when execute method ,message {}",e.getMessage());

return new ResponseBean<>(false, UnicomResponseEnums.CONNECTION_ERROR);

}

@ExceptionHandler(value={Exception.class})

@ResponseBody

@ResponseStatus(value=HttpStatus.METHOD_NOT_ALLOWED)

public ResponseBean<String> notAllowed(Exception e){

logger.error("occurs error when execute method ,message {}",e.getMessage());

return new ResponseBean<>(false, UnicomResponseEnums.METHOD_NOT_ALLOWED);

}

}

建立一个测试controller类:

package com.unicom.microservice.peixun.peixun_matb3.web;/**

* Created by matengbing on 2018/10/15.

*/

import com.unicom.microservice.peixun.peixun_matb3.Response.ResponseBean;

import com.unicom.microservice.peixun.peixun_matb3.enums.UnicomResponseEnums;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;

import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;

import java.util.Map;

import java.util.Objects;

import java.util.StringJoiner;

/**

* @program: 测试

* @description:测试接口

* @author: mtb

* @create: 2018-10-15 11:26

**/

@RestController

@ApiIgnore

//@RequestMapping(value = "sayHello")

public class HelloController {

@CrossOrigin

@ApiOperation(value = "hello")

@RequestMapping(value = "/hello",method = RequestMethod.GET)

public ResponseBean<UnicomResponseEnums> sayHello(){

ResponseBean responseBean=new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);

return responseBean;

}

}

使用postman测试接口:

注意要配置springboot启动类,扫描controller等注解类被扫描到

@API等注解是swagger的注解,如果没有使用到可以删除

我们在实现数据库操作的方法中try,catch,捕捉到异常后,抛出一个自定义异常

@Transactional

@Override

public int addUser(User user) {

try {

user.setUserpwd(DigestUtils.md5DigestAsHex(user.getUserpwd().getBytes()));

userMapper.insertUser(user);

return user.getId();

}catch (Exception e){

throw new UnicomRuntimeException(UnicomResponseEnums.FAIL_INSERTUSER);

}

}

接着拦截404错误

package com.unicom.peixun.web;

import com.unicom.peixun.Response.ResponseBean;

import com.unicom.peixun.enums.UnicomResponseEnums;

import org.springframework.boot.autoconfigure.web.ErrorController;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**

* @program: peixun_matb3

* @description:

* @author: matengbing

* @create: 2018-10-20 09:08

**/

@RestController

public class MyBasicErrorController implements ErrorController {

private static final String ERROR_PATH="/error";

@RequestMapping("/error")

public ResponseBean<Object> handleError(HttpServletRequest request){

//获取statusCode:401,404,500

Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");

if(statusCode==404){

return new ResponseBean<>(false,null,UnicomResponseEnums.NOT_FOUND);

}else {

return new ResponseBean<>(false,null,UnicomResponseEnums.METHOD_NOT_ALLOWED);

}

}

@Override

public String getErrorPath() {

return ERROR_PATH;

}

}

用于登录拦截的拦截器实现返回restful数据:

package com.unicom.peixun.inteceptor;

import com.alibaba.fastjson.JSON;

import com.unicom.peixun.Response.ResponseBean;

import com.unicom.peixun.domain.User;

import com.unicom.peixun.enums.UnicomResponseEnums;

import com.unicom.peixun.web.UserController;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.web.servlet.HandlerInterceptor;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import java.io.PrintWriter;

/**

* @program: peixun_matb3

* @description:

* @author: matengbing

* @create: 2018-10-18 10:40

**/

@Component

public class LoginInterceptor implements HandlerInterceptor {

private static Logger logger=Logger.getLogger(LoginInterceptor.class);

@Autowired

UserController userController;

// 在请求处理之前调用,只有返回true才会执行请求

@Override

public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

HttpSession session=httpServletRequest.getSession();

User user=(User) session.getAttribute("user");

//登录过得用户直接通过

if(user!=null){

return true;

}

//未登录用户拦截,返回json数据

httpServletResponse.setCharacterEncoding("UTF-8");

httpServletResponse.setContentType("application/json; charset=utf-8");

PrintWriter out = httpServletResponse.getWriter() ;

try{

ResponseBean<Object> responseBean=new ResponseBean<>(false,null,UnicomResponseEnums.NOLOGIN);

out = httpServletResponse.getWriter();

out.append(JSON.toJSON(responseBean).toString());

return false;

}

catch (Exception e) {

e.printStackTrace();

httpServletResponse.sendError(500);

return false;

}

}

@Override

public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

}

@Override

public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

}

}

拦截器需要配置@configuration

package com.unicom.peixun.configuration;

import com.unicom.peixun.inteceptor.LoginInterceptor;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**

* @program:

* @description:

* @author: mtb

* @create: 2018-10-18 10:50

**/

@Configuration

public class LoginAdapter extends WebMvcConfigurerAdapter {

@Autowired

LoginInterceptor loginInterceptor;

@Override

public void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(loginInterceptor)

.addPathPatterns("/**")

.excludePathPatterns("/login")

.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");

super.addInterceptors(registry);

}

/**

* 配置静态访问资源

* @param registry

*/

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

registry.addResourceHandler("swagger-ui.html")

.addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars/**")

.addResourceLocations("classpath:/META-INF/resources/webjars/");

super.addResourceHandlers(registry);

}

}

配置了拦截器拦截所有请求,不拦截/login,swagger的相关请求,静态资源的请求,

如果拦截了/login会出现循环重定向报错

如果拦截了swagger的相关请求,查看swagger文档时看不到相应资源

喜欢的点点关注,点点赞。

对Java技术,架构技术感兴趣的同学,欢迎加QQ群585550789,一起学习,相互讨论。

群内已经有小伙伴将知识体系整理好(源码,笔记,PPT,学习视频),欢迎加群领取。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容

  • 很久没有过年的感觉了,不管是在南方的广东还是在老家,都找不到以前过年的那种感觉,就像现在吃什么都吃不出儿时的味道一...
    孤独园主阅读 94评论 0 0
  • 想一拥而就蓝天,替换抱你的双臂 看鹰飞略树的影子,对映红豆相思 等你的心情如候冷风过境,雨城雨兮 一如往常的遥遥无...
    小苏尔阅读 357评论 9 5
  • 如果把学生比作我的孩子的话,那对于去年开学接手的八一班来说,我无疑是个后妈了。 你结婚了只是当一个孩子或者顶多三个...
    脁登阅读 573评论 3 4
  • 今天中午午睡时做梦,梦见自己遇到了一个九十多岁已经退休的资深的大夫,向她请教如何去医院看病的问题。她虽然年事已高但...
    津城燕窝Donna阅读 146评论 0 0