目录
一、SpringMVC对Json的支持
1.1导入pom依赖
1.2 配置Springmvc-Servlet.xml适配器
1.3、JSON的各种场景
1.1.1返回List
1.1.2返回List
1.1.3返回T
1.1.4返回Map
1.1.5返回混合
1.1.6返回JSON字符串
二、SpringMVC全局异常处理
2.1为什么要全局异常处理
2.2异常处理思路
2.3SpringMVC异常分类
2.4非全局异常处理问题演示
2.2 SpringMVC自带的简单异常处理器
2.3 通过HandlerExceptionResovler接口实现全局异常
2.4 使用@ControllerAdvice+@ExceptionHandler实现全局异常
2.5 全局异常处理JSON返回
{ }--->json对象、[ ]-->Json数组
{
msg:" "
data:[ ]
}
上述三种情况 SpringMVC框架如何才能产生以上三种情况:
1.导入pom依赖
2.配置SprongMVC.xml配置适配器,作用事用来做Json数据转换
3.使用@responseBody能够将任何数据转换成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
// list
@ResponseBody
@RequestMapping("/List")
public List list(HttpServletRequest request,Clazz clazz){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
// [{},{}]
return this.ClazzBiz.listPager(clazz,pageBean);
}
// list
// T
@ResponseBody
@RequestMapping("/load")
public Clazz load(HttpServletRequest request, Clazz clazz){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
// [{},{}]
return this.ClazzBiz.listPager(clazz,pageBean).get(0);
}
// map
@ResponseBody
@RequestMapping("/map")
public Map map(HttpServletRequest request, Clazz clazz){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
// [{},{}]
return this.ClazzBiz.listMapPager(clazz,pageBean).get(0);
}
//混合对象
@ResponseBody
@RequestMapping("/all")
public Map all(HttpServletRequest request, Clazz clazz){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
List list = this.ClazzBiz.listPager(clazz, pageBean);
Map map = new HashMap();
map.put("data",list);
map.put("pagebean",pageBean);
return map;
}
@ResponseBody
@RequestMapping("/list")
public String list(){
System.out.println("JsonController.clzEdit");
return "clzEdit";
}
@ResponseBody+@controller = @restController
系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。在开发中,不管是dao层、service层还是controller层,都有可能抛出异常,在springmvc中,能将所有类型的异常处理从各处理过程解耦出来,既保证了相关处理过程的功能较单一,也实现了异常信息的统一处理和维护
系统的dao、service、controller出现异常都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理。springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理
1、使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver
2、实现Spring的异常处理接口HandlerExceptionResolver自定义自己的异常处理器
3、使用@ControllerAdvice + @ExceptionHandler
Controller层
// list
@ResponseBody
@RequestMapping("/List")
public List list(HttpServletRequest request,Clazz clazz){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
if(true)
throw new RuntimeException("查询班级信息异常,异常错误出现在 JsonController....");
// [{},{}]
List list = this.ClazzBiz.listPager(clazz,pageBean);
return list;
}
// list格式
@ResponseBody
@RequestMapping("/listMap")
public List listMap(HttpServletRequest request, Clazz clazz){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
// [{},{}]
List list = this.ClazzBiz.listMapPage(clazz, pageBean);
return list;
}
ClazzBizImpl:
@Override
public List listMapPage(Clazz clazz, PageBean pageBean) {
if (true)
throw new RuntimeException("查询班级信息异常,异常错误出现在 ClazzBizImpl.maplistPager....");
return clazzMapper.listMapPage(clazz);
}
测试:
错误报在controller层
错误报在service层
Springmvc-Servlet.xml
error
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
${ex}
新建GlobalException类:
package com.zwc.ssm.exception;
/**
* @authorzwc
* @site www.javazwc.com
* @company xxx公司
* @create 2022-08-23 2:12
*/
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.zwc.ssm.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;
/**
* @authorzwc
* @site www.javazwc.com
* @company xxx公司
* @create 2022-08-23 2:15
*/
@Component
public class GlobalHandlerExceptionResovler implements HandlerExceptionResolver {
/**
*
* @param httpServletRequest
* @param httpServletResponse
* @param o 目标对象
* @param e 目标对象执行,出现的异常对象
* @return
*/
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
if(e instanceof GlobalException){
GlobalException exception =(GlobalException) e;
mv.addObject("ex","null");
mv.addObject("msg","全局异常,错误码501");
}else if (e instanceof RuntimeException){
RuntimeException exception = (RuntimeException) e;
mv.addObject("ex",exception.getMessage());
mv.addObject("msg","运行时异常,错误码601");
}
return mv;
}
}
测试代码controller层的改变,改变load方法
@ResponseBody
@RequestMapping("/load")
public Clazz load(HttpServletRequest request, Clazz clazz){
if(clazz.getCid()!=null){
List list = this.ClazzBiz.listPager(clazz, null);
if(true)
throw new GlobalException("系统繁忙,请参考E007");
return list.get(0);
}
// [{},{}]
return null;
}
测试:
运行时异常
全局异常
新建GlobalExceptionResolver类
package com.zwc.ssm.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
/**
* @authorzwc
* @site www.javazwc.com
* @company xxx公司
* @create 2022-08-23 14:45
*/
@ControllerAdvice
public class GlobalExceptionResolver {
@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","全局异常,错误码501");
}else if (e instanceof RuntimeException){
RuntimeException runtimeException = (RuntimeException) e;
mv.addObject("ex",runtimeException.getMessage());
mv.addObject("msg","运行时异常,错误码601");
}
return mv;
}
}
测试:
新建GlobalExceptionResolver2
package com.zwc.ssm.exception;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;
/**
* @authorzwc
* @site www.javazwc.com
* @company xxx公司
* @create 2022-08-23 14:45
*
* 当出现异常,统一向前端响应错误信息的json对象数据
*/
@RestControllerAdvice
public class GlobalExceptionResolver2 {
@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;
}
}
测试:
今天的内容就到这里了,若有问题请各位大佬指正