统一异常处理:
统一的异常
public class BusinessException extends RuntimeException{
private int code;
private String msg;
BusinessException(ResultCode resultCode){
this.code=resultCode.getCode();
this.msg=resultCode.getMsg();
}
public int getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
统一的返回:
public class R {
private int code;
private String msg;
public R(int code, String msg) {
this.code = code;
this.msg = msg;
}
public static R of(ResultCode resultCode){
return new R(resultCode.getCode(),resultCode.getMsg());
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
resultcode枚举类
public enum ResultCode {
/**
* 成功 -----------------每个枚举都是一个单例
*/
SUCCESS(200, "success"),
/**
* 未知错误
*/
UNKNOWN_ERROR(10001, "unkonwn error"),
/**
* 用户名错误或不存在
*/
USERNAME_ERROR(10002, "username error or does not exist"),
/**
* 密码错误
*/
PASSWORD_ERROR(10003, "password error"),
/**
* 用户名不能为空
*/
USERNAME_EMPTY(10004, "username can not be empty"), //★没结束前都用逗号!
/**
* 业务统一异常
*/
BUSINESS_ERROR(10005, "业务发生了问题");
/**
* 结果码
*/
private Integer code;
/**
* 结果码描述
*/
private String msg;
ResultCode(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public String getMsg() {
return msg;
}
}
全局异常处理类:
@ControllerAdvice
public class GlobalExceptionResolver {
@ExceptionHandler(BusinessException.class) //将来businessExceptionHandler就全局捕获,处理此异常
@ResponseBody //返回json,直接放到响应体中
public R businessExceptionHandler(){
return R.of(ResultCode.BUSINESS_ERROR);
}
@ExceptionHandler(Exception.class) //自定义异常都没捕获到时,抛父异常
@ResponseBody //返回json,直接放到响应体中
public R exceptionHandler(){
return R.of(ResultCode.UNKNOWN_ERROR);
}
}
自定义转换器:
场景:前端复杂的字段比如Date类型如何转换。
自定义转化器类:
public class StringToDate implements Converter<String, Date> {
@Override
public Date convert(String s) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd hh,mm,ss");
try {
return simpleDateFormat.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
配置
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean id="stringToDate" class="cn.itnls.convertor.StringToDate"/>
</set>
</property>
</bean>
controller方法:
@PostMapping("showUser")
@ResponseBody
public User showUser( User user) {
System.out.println(user);
return user;
}
user.http测试:
POST http://localhost:8080/user/showUser
#Content-Type: application/json
Content-Type: application/x-www-form-urlencoded
id=1&username=zs&password=123&birthday=2020_1_18 12,12,12
★json转换器(使用jackson的注解):
使用**@Jsonfomat** 能达到双向转换(前台入参格式化/后台出参格式化),前提是请求必须从requestBody/responseBody中获取的。
@Jsonformat:对象和json转化过程中按照此方式转化(出入参都能实现转化)
@DateTimeFormat:从requestParam中获取参数并转化,且自定义的转化器也是从requestParam中获取(专门做入参)
★以上两种注解最好全在字段加上!
假如传来的复杂属性如Date就是json:
只需在属性加一个jackson注解:
注意使用jackson要注意**时区!**国外的依赖默认的和咱的不一样
public class User {
private String username;
private String password;
//假如入参不是json是urlencoded,那再加@DateTimeFormat(pattern="xx")
@JsonFormat(pattern = "yyyy年MM月dd日",timezone = "GMT-8")
private Date birthday;
}
前台请求测试:
POST http://localhost:8080/user/showUser
Content-Type: application/json
{
"id": 999,
"username": "content",
"password": "1233",
"birthday": "2020年1月23日"
}
本文介绍了Springmvc中如何进行统一的异常处理,包括全局异常处理类和resultcode枚举类的使用。同时,详细阐述了自定义转换器的创建和配置,特别是针对日期类型的JSON转换,利用Jackson的@JsonFormat和@DateTimeFormat注解实现日期的自动格式化处理,确保前端和后端的数据交互顺利进行。
172

被折叠的 条评论
为什么被折叠?



