common包下,json,异常处理
程序员文章站
2024-01-10 10:20:52
...
JsonData
@Getter
@Setter
public class JsonData {
private boolean ret;
private String msg;
private Object data;
public JsonData(boolean ret) {
this.ret = ret;
}
public static JsonData success(Object object, String msg) {
JsonData jsonData = new JsonData(true);
jsonData.data = object;
jsonData.msg = msg;
return jsonData;
}
public static JsonData success(Object object) {
JsonData jsonData = new JsonData(true);
jsonData.data = object;
return jsonData;
}
public static JsonData success() {
return new JsonData(true);
}
public static JsonData fail(String msg) {
JsonData jsonData = new JsonData(false);
jsonData.msg = msg;
return jsonData;
}
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<String, Object>();
result.put("ret", ret);
result.put("msg", msg);
result.put("data", data);
return result;
}
}
全局异常处理 SpringExceptionResolver
引入包:
<!-- jsp api -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>jsp-api</artifactId>
<version>6.0.36</version>
</dependency>
@Slf4j
public class SpringExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
String url = request.getRequestURL().toString();
ModelAndView mv;
String defaultMsg = "System error";
// 这里我们要求项目中所有请求json数据,都使用.json结尾
if (url.endsWith(".json")) {
if (ex instanceof PermissionException || ex instanceof ParamException) {
JsonData result = JsonData.fail(ex.getMessage());
mv = new ModelAndView("jsonView", result.toMap());
} else {
log.error("unknown json exception, url:" + url, ex);
JsonData result = JsonData.fail(defaultMsg);
mv = new ModelAndView("jsonView", result.toMap());
//对应spring-servlet.xml <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
}
} else if (url.endsWith(".page")){ // 这里我们要求项目中所有请求page页面,都使用.page结尾
log.error("unknown page exception, url:" + url, ex);
JsonData result = JsonData.fail(defaultMsg);
mv = new ModelAndView("exception", result.toMap());
//返回到 exception.jsp 页面
} else {
log.error("unknow exception, url:" + url, ex);
JsonData result = JsonData.fail(defaultMsg);
mv = new ModelAndView("jsonView", result.toMap());
}
return mv;
}
}
spring-servlet.xml配置,才能使用
<bean class="com.mmall.common.SpringExceptionResolver" />
自定义异常
public class PermissionException extends RuntimeException {
public PermissionException() {
super();
}
public PermissionException(String message) {
super(message);
}
public PermissionException(String message, Throwable cause) {
super(message, cause);
}
public PermissionException(Throwable cause) {
super(cause);
}
protected PermissionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
测试:
@Controller
@RequestMapping("/test")
@Slf4j
public class TestController {
@RequestMapping("/hello.json")
@ResponseBody
public JsonData hello() {
log.info("hello");
throw new PermissionException("test exception");
{
ret:false,
msg:"test exception",
data:null
}
// return JsonData.success("hello, permission");
{
ret:true,
msg:null,
data:"hello,permission"
}
}
}
推荐阅读
-
common包下,json,异常处理
-
SpringBoot学习之全局异常处理设置(返回JSON)
-
SpringMVC入门(二)—— 参数的传递、Controller方法返回值、json数据交互、异常处理、图片上传、拦截器
-
php中对内置函数json_encode和json_decode的异常处理
-
SpringBoot学习之全局异常处理设置(返回JSON)
-
SpringMVC入门(二)—— 参数的传递、Controller方法返回值、json数据交互、异常处理、图片上传、拦截器
-
jackson在处理json时发生错误:死循环报BeanSerializer 异常处理方法
-
微信小程序中如何解决wx.request对于JSON 含\u2028的处理异常
-
php中对内置函数json_encode和json_decode的异常处理
-
微信小程序中如何解决wx.request对于JSON 含\u2028的处理异常