目录
一、json
1、导入pom依赖
2、springmvc-servlet.xml配置
3、mapper.xml
4、servlet层
5、后台代码
6、前台代码
7、效果
二、全局异常处理
1.为什么要全局异常处理
2.异常处理思路
3.SpringMVC异常分类
4、SpringMVC自带的简单异常处理器
5、通过HandlerExceptionResovler接口实现全局异常
6、使用@ControllerAdvice+@ExceptionHandler实现全局异常
7、全局异常处理JSON返回
com.fasterxml.jackson.core
jackson-databind
2.9.3
com.fasterxml.jackson.core
jackson-core
2.9.3
com.fasterxml.jackson.core
jackson-annotations
2.9.3
text/html;charset=UTF-8
text/json;charset=UTF-8
application/json;charset=UTF-8
public List
package com.cxy.controller;
import com.cxy.biz.ClazzBiz;
import com.cxy.model.Clazz;
import com.cxy.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//@ResponseBody
//@Controller
@RestController
@RequestMapping("/clz/json")
public class JsonController {
@Autowired
private ClazzBiz clazzBiz;
/**
* 返回JSON字符串
*/
@RequestMapping("/clzEdit")
public String clzEdit(){
return "JosnController.clzEdit";
}
/**
* 返回List
*/
@RequestMapping("/list")
public List list(Clazz clazz, HttpServletRequest req){
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
if(true)
throw new RuntimeException("查询班级的信息");
List lst = this.clazzBiz.listPager(clazz, pageBean);
return lst;
}
/**
* 返回T
*/
@RequestMapping("/load")
public Clazz load(HttpServletRequest req, Clazz clazz){
if(clazz.getCid() != null){
List lst = this.clazzBiz.listPager(clazz, null);
return lst.get(0);
}
return null;
}
/**
* 返回List
返回List<T>
返回List<Map>
返回T
返回Map
返回混合
返回JSON字符串
返回list
系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。在开发中,不管是dao层、service层还是controller层,都有可能抛出异常,在springmvc中,能将所有类型的异常处理从各处理过程解耦出来,既保证了相关处理过程的功能较单一,也实现了异常信息的统一处理和维护。
系统的dao、service、controller出现异常都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理。springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。
1)使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver;
2)实现Spring的异常处理接口HandlerExceptionResolver自定义自己的异常处理器;
3)使用@ControllerAdvice + @ExceptionHandler
JsonControler.java
/**
* 返回List
*/
@RequestMapping("/list")
public List list(Clazz clazz, HttpServletRequest req){
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
if(true) {
throw new RuntimeException("查询班级的信息异常,异常处在于JsonController.list方法中");
}
List lst = this.clazzBiz.listPager(clazz, pageBean);
return lst;
}
错误报在controller层
BizImpl.java
@Override
public List MaplistPager(Clazz clazz,PageBean pageBean) {
if (true)
throw new RuntimeException("错误出现在 ClazzBizImpl.mapListPager...");
return clazzMapper.MaplistPager(clazz);
}
错误出现在service层
springmvc.xml
error
Error.jsp
${ex}
结果
全局异常GlobalException
package com.cxy.Exception;
public class GlobalException extends RuntimeException {
public GlobalException() {
}
public GlobalException(String message) {
super(message);
}
public GlobalException(String message, Throwable cause) {
super(message, cause);
}
public GlobalException(Throwable cause) {
super(cause);
}
public GlobalException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
异常处理器 GlobalExceptionHandler
package com.cxy.Exception;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class GlobalExceptionHandler implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object o, Exception e) {
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
if (e instanceof GlobalException){
GlobalException globalException = (GlobalException) e;
mv.addObject("ex",globalException.getMessage());
mv.addObject("msg","全局异常,错误码501....");
}else if (e instanceof RuntimeException){
RuntimeException runtimeException = (RuntimeException) e;
mv.addObject("ex",runtimeException.getMessage());
mv.addObject("msg","运行时异常,错误码601....");
}
return mv;
}
}
测试代码controller层的改变
/**
* 返回T
*/
@ResponseBody
@RequestMapping("/load")
public Clazz load(HttpServletRequest req, Clazz clazz){
if(clazz.getCid() != null){
List lst = this.clazzBiz.listPager(clazz, null);
if (true)
throw new GlobalException("错误出现在 JsonController.load...");
return lst.get(0);
}
return null;
}
结果:
package com.cxy.Exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
public class GlobalExceptionResovler {
@ExceptionHandler
public ModelAndView handler(Exception e){
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
if (e instanceof GlobalException){
GlobalException globalException = (GlobalException) e;
mv.addObject("ex",globalException.getMessage());
mv.addObject("msg","全局异常....");
}else if (e instanceof RuntimeException){
RuntimeException runtimeException = (RuntimeException) e;
mv.addObject("ex",runtimeException.getMessage());
mv.addObject("msg","运行时异常....");
}
return mv;
}
}
package com.cxy.Exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;
/**
* 当出现异常,统一向前端响应错误后出现的结果
*/
//@ControllerAdvice
@RestControllerAdvice
public class GlobalExceptionJsonResolver {
@ExceptionHandler
public Map handler(Exception e){
Map map = new HashMap();
if (e instanceof GlobalException){
GlobalException globalException = (GlobalException) e;
map.put("ex",globalException.getMessage());
map.put("msg","全局异常 GlobalExceptionResolver,错误码501....");
}else if (e instanceof RuntimeException){
RuntimeException runtimeException = (RuntimeException) e;
map.put("ex",runtimeException.getMessage());
map.put("msg","运行时异常 GlobalExceptionResolver,错误码601....");
}
return map;
}
}
结果