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
完成以下操作首先要在类上添加注解
package com.yzp.controller;
import com.yzp.biz.ClayyBiz;
import com.yzp.exception.GlobalException;
import com.yzp.model.Clayy;
import com.yzp.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;
/**
* @author易泽鹏
* @site 16670388677
* @company
* @create 2022-08-22 19:38
*/
@RestController
@RequestMapping("/cly/json")
public class JsonController {
@Autowired
private ClayyBiz clayyBiz;
//list
@RequestMapping("/list")
public List list(HttpServletRequest request, Clayy clayy){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
//[{}{}]
return this.clayyBiz.listPager(clayy,pageBean);
}
jsp
返回list<T>对象
后台代码
//list
jsp代码
返回list<map>对象
后台代码
/**
* 返回Map
* @param req
* @param clayy
* @return
*/
@ResponseBody
@RequestMapping("/mapLoad")
public Map mapLoad(HttpServletRequest req, Clayy clayy){
if(clayy.getCid() != null){
List lst = this.clayyBiz.listMapPager(clayy, null);
System.out.println(lst.get(0));
return lst.get(0);
}
return null;
}
后台代码
返回T对象
//clayy
@RequestMapping("/load")
public Clayy load(HttpServletRequest request, Clayy clayy){
//http://localhost/cly/json/load?cid=1
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
//{}
System.out.println(this.clayyBiz.listPager(clayy, pageBean).size());
return this.clayyBiz.listPager(clayy,pageBean).get(0);
}
后台
//clayy
@RequestMapping("/hunhe")
public Map hunhe(HttpServletRequest request, Clayy clayy){
//http://localhost/cly/json/load?cid=1
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
//[{}{}]
Map map = new HashMap();
List lst = this.clayyBiz.listPager(clayy, pageBean);
map.put("lst",lst);
map.put("pagebean",pageBean);
return map;
}
jsp代码
返回hunhe对象
@RequestMapping("/clyedit")
public String clyedit(){
System.out.println("JsonController.clyedit");
return "clyedit";
}
我们知道,系统中异常包括:编译时异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。在开发中,不管是dao层、service层还是controller层,都有可能抛出异常,在springmvc中,能将所有类型的异常处理从各处理过程解耦出来,既保证了相关处理过程的功能较单一,也实现了异常信息的统一处理和维护。
系统的dao、service、controller出现异常都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理。springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。
1)使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver;
2)实现Spring的异常处理接口HandlerExceptionResolver自定义自己的异常处理器;
3)使用@ControllerAdvice + @ExceptionHandler
SpringMVC的配置文件:
error
Error.jsp
${ex}
后台代码:
//list
@RequestMapping("/list")
public List list(HttpServletRequest request, Clayy clayy){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
if(true)
throw new RuntimeException("查询班级异常,异常存在于JsonController.list");
//[{}{}]
return this.clayyBiz.listPager(clayy,pageBean);
}
//list
@RequestMapping("/listmap")
public List listmap(HttpServletRequest request, Clayy clayy){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
//[{}{}]
if(true)
throw new RuntimeException("查询班级异常,异常存在于ClayyBizImpl.list");
return this.clayyBiz.listMapPager(clayy,pageBean);
}
先将上面SpringMVC中关于异常处理的配置取消掉
全局异常GlobalException
package com.yzp.exception;
/**
* @author易泽鹏
* @site 16670388677
* @company
* @create 2022-08-22 21:27
*/
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.yzp.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;
/**
* @author易泽鹏
* @site 16670388677
* @company
* @create 2022-08-22 21:31
*
* 处理全局异常的解析器
*/
@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",exception.getMessage());
mv.addObject("msg","全局异常,错误码001");
}else if(e instanceof RuntimeException) {
RuntimeException exception = (RuntimeException) e;
mv.addObject("ex", exception.getMessage());
mv.addObject("msg", "运行时异常,错误码701");
}
return mv;
}
}
//clayy
@RequestMapping("/load")
public Clayy load(HttpServletRequest request, Clayy clayy){
//http://localhost/cly/json/load?cid=1
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
//{}
if(true)
throw new GlobalException("系统繁忙,请参考001");
System.out.println(this.clayyBiz.listPager(clayy, pageBean).size());
return this.clayyBiz.listPager(clayy,pageBean).get(0);
}
显示运行时异常 :RuntimeException
//list
@RequestMapping("/list")
public List list(HttpServletRequest request, Clayy clayy){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
if(true)
throw new RuntimeException("查询班级异常,异常存在于JsonController.list");
//[{}{}]
return this.clayyBiz.listPager(clayy,pageBean);
}
//list
@RequestMapping("/listmap")
public List listmap(HttpServletRequest request, Clayy clayy){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
//[{}{}]
if(true)
throw new RuntimeException("查询班级异常,异常存在于ClayyBizImpl.list");
return this.clayyBiz.listMapPager(clayy,pageBean);
}
先建一个GlobalExceptionResolver的类
package com.yzp.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
/**
* @author易泽鹏
* @site 16670388677
* @company
* @create 2022-08-23 17:09
*/
@ControllerAdvice
public class GlobalExceptionResolver {
@ExceptionHandler
public ModelAndView handler(Exception e){
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
if(e instanceof GlobalException){
GlobalException exception = (GlobalException) e;
mv.addObject("ex",exception.getMessage());
mv.addObject("msg","全局异常,GlobalExceptionResolver,错误码001");
}else if(e instanceof RuntimeException) {
RuntimeException exception = (RuntimeException) e;
mv.addObject("ex", exception.getMessage());
mv.addObject("msg", "运行时异常,GlobalExceptionResolver,错误码701");
}
return mv;
}
}
@ControllerAdvice+@ExceptionHandler这两个注解是关键
把GlobalExceptionResolver的@ControllerAdvice注释了
然后再建一个GlobalExceptionResolver2 类
package com.yzp.exception;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;
/**
* @author易泽鹏
* @site 16670388677
* @company
* @create 2022-08-23 17:09
*当出现异常,统一向前端响应错误信息的json数据对象
*/
//@ControllerAdvice
@RestControllerAdvice
public class GlobalExceptionResolver2 {
@ExceptionHandler
public Map handler(Exception e){
Map map = new HashMap();
if(e instanceof GlobalException){
GlobalException exception = (GlobalException) e;
map.put("ex",exception.getMessage());
map.put("msg","全局异常,GlobalExceptionResolver,错误码001");
}else if(e instanceof RuntimeException) {
RuntimeException exception = (RuntimeException) e;
map.put("ex", exception.getMessage());
map.put("msg", "运行时异常,GlobalExceptionResolver,错误码701");
}
return map;
}
}
运行结果: