JSON的形式:
{}-->json对象
[]-->json数组
常见的就是对象套数组:
{
msg:""
code:200,
data[]
}--->json混合对象
2.SpringMVC框架如何产生上述三种格式的数据
1.导入pom依赖 -----------Jackson
2.配置(Spring-mvc.xml)适配器
作用:做json数据的转换
3.使用注解@responseBody能够将任何数据转成json对象
1.导入pom依赖 -----------Jackson
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
2.配置(Spring-mvc.xml)适配器
这是固定的配置
text/html;charset=UTF-8
text/json;charset=UTF-8
application/json;charset=UTF-8
后台编写
package com.zhouzhou.ssm.controller;
import com.zhouzhou.ssm.Biz.ClazzBiz;
import com.zhouzhou.ssm.exception.GlobalException;
import com.zhouzhou.ssm.mapper.ClazzMapper;
import com.zhouzhou.ssm.model.Clazz;
import com.zhouzhou.ssm.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
**
* @author周周
* @company zsm
* @create 2022-08-22 20:39
*/
@RestController
@RequestMapping("clz/json")
public class JsonController {
@Autowired
private ClazzBiz clazzBiz;
@RequestMapping("/clzEdit")
public String clzEdit(){
System.out.println("JsonController.clzEdit");
return "clzEdit";
}
//返回list
@RequestMapping("/list")
public List list(HttpServletRequest request, Clazz clazz){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
//自动返回json数组
return this.clazzBiz.listPager(clazz,pageBean);
}
//返回list
解析:
@RequestMapping:窄化路径
@ResponseBody:区分注解(添加它后,会绕过本地视图解析器)
@ResponseBody+@Controller=@RestController
业务逻辑层
public List mapListPager(Clazz clazz,PageBean pageBean) {
return clazzMapper.mapListPager(clazz);
}
Mapper.xml(自动生成的xml文件)
测试
建一个jsp界面
<%--
Created by IntelliJ IDEA.
User: zjjt
Date: 2022/8/22
Time: 18:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
测试json数据返回
返回list<T>对象
返回list<Map>对象
返回T对象
返回Map对象
返回混合对象
《效果图》
1.非全局异常处理问题演示
异常处理的三种形式:
1.Springmvc.xml中进行统一配置及处理
2.实现接口管理
3.@ControllerAdvice
异常处理的思路:
参考images中的《springmvc全局异常处理思路.png》
系统的dao、service、controller出现异常都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理。springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。
SpringMVC异常分类:
使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver
实现Spring的异常处理接口HandlerExceptionResolver自定义自己的异常处理器;
使用@ControllerAdvice + @ExceptionHandler
代码演示
@RequestMapping("/load")
public Clazz load(HttpServletRequest request,Clazz clazz){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
if(true)
throw new GlobalException("系统繁忙,请参考E007;");
return this.clazzBiz.listPager(clazz,pageBean).get(0);
}
@Override
public List listMapPager(Clazz clazz, PageBean pageBean) {
if(true)
throw new RuntimeException("系统繁忙!异常存在于ClazzBizImpl中list方法");
return clazzMapper.listMapPager(clazz);
}
2,全局异常处理
SpringMVC自带的简单异常处理器
error
出错时会自动跳到指定的jsp界面,怎么展示取决于以上配置如何配
<%--
Created by IntelliJ IDEA.
User: zjjt
Date: 2022/8/22
Time: 18:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
错误信息页面
${msg}
${ex}
通过HandlerExceptionResovler接口实现全局异常
提示:在运行之前需将SpringMVC中关于异常处理的配置取消掉
package com.zhouzhou.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;
**
* @author周周
* @company zsm
* @create 2022-08-22 22:39
*/
//注释掉为了运行时不受它的影响
//@Component
public class GlobalHandlerExceptionResolver 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","全局异常,错误码501");
}else if(e instanceof RuntimeException){
RuntimeException exception = (RuntimeException) e;
mv.addObject("ex",exception.getMessage());
mv.addObject("msg","运行时异常,错误码601");
}
return mv;
}
}
测试代码的改变
//返回T
@RequestMapping("/load")
public Clazz load(HttpServletRequest request,Clazz clazz){
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
if(true)
throw new GlobalException("系统繁忙,请参考E007;");
return this.clazzBiz.listPager(clazz,pageBean).get(0);
}
jsp界面
<%--
Created by IntelliJ IDEA.
User: zjjt
Date: 2022/8/22
Time: 18:43
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
错误信息页面
${msg}
${ex}
《效果图》
使用@ControllerAdvice+@ExceptionHandler实现
package com.zhouzhou.ssm.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
**
* @author周周
* @company zsm
* @create 2022-08-22 19:39
*/
@ControllerAdvice
public class GlobalExceptionResolver {
@ExceptionHandler
public ModelAndView hadler(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,错误码501");
}else if(e instanceof RuntimeException){
RuntimeException exception = (RuntimeException) e;
mv.addObject("ex",exception.getMessage());
mv.addObject("msg","运行时异常GlobalExceptionResolver,错误码601");
}
return mv;
}
}
全局异常处理JSON返回
package com.zhouzhou.ssm.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
**
* @author周周
* @company zsm
* @create 2022-08-22 19:39
* 当出现异常,统一向前端响应错误信息的json对象数据
*/
@RestControllerAdvice
public class GlobalExceptionResolver2 {
@ExceptionHandler
public Map hadler(Exception e){
Map map = new HashMap();
if(e instanceof GlobalException){
GlobalException exception =(GlobalException) e;//强转
map.put("ex",exception.getMessage());
map.put("msg","全局异常GlobalExceptionResolver,错误码501");
}else if(e instanceof RuntimeException){
RuntimeException exception = (RuntimeException) e;
map.put("ex",exception.getMessage());
map.put("msg","运行时异常GlobalExceptionResolver,错误码601");
}
return map;
}
}