ShanTianQi

SpringMVC

SpringMVC


第一章 SpingMVC简介


1.1SpringMVC概述

优点:

20221225135616 20221225135735

传统Servlet

package com.priv.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author : 十一
 * @data : 13:45 2022/12/17
 * When in doubt, use brute force.
 */
@WebServlet("/user/delete")
public class UserDeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接受请求参数
        String name = req.getParameter("name");
        System.out.println("servlet delete name ==>" + name);
        //2.产生响应
        resp.setContentType("text/json;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        pw.write("{'module' : 'servlet delete'}");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

SpringMVC

package com.priv.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author : 十一
 * @data : 14:05 2022/12/17
 * When in doubt, use brute force.
 */
@Controller
public class UserController {

    @RequestMapping("/save")
    @ResponseBody
    public String save(String name){
        System.out.println("springmvc save name ==> " + name);
        return "{'module' : 'springmvc save'}";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String name){
        System.out.println("springmvc delete name ==> " + name);
        return "{'module' : 'springmvc delete'}";
    }

    @RequestMapping("/select")
    @ResponseBody
    public String select(String name){
        System.out.println("springmvc select name ==> " + name);
        return "{'module' : 'springmvc select'}";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(String name){
        System.out.println("springmvc update name ==> " + name);
        return "{'module' : 'springmvc update'}";
    }
}

1.2入门案例

一、先导入SpringMVC和Servlet的坐标

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>6.0.3</version>
</dependency>

二、创建SpringMVC控制器类(等同于Servlet功能)

package com.priv.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author : 十一
 * @data : 14:51 2022/12/25
 * When in doubt, use brute force.
 */
@Controller
public class UserController {

    @RequestMapping("/save")
    @ResponseBody
    public String save(){
        System.out.println("user save ...");
        return "{'module' : 'springmvc'}";
    }
}

三、初始化SpringMVC环境(同Spring环境),设定SpringMVC加载对应的bean

package com.priv.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author : 十一
 * @data : 14:54 2022/12/25
 * When in doubt, use brute force.
 */
@Configuration
@ComponentScan("com.priv.controller")
public class SpringMvcConfig {
}

四、初始化Servlet容器,加载SpringMVC环境,并设置SpringMVC技术处理的请求

package com.priv.config;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

/**
 * @author : 十一
 * @data : 14:56 2022/12/25
 * When in doubt, use brute force.
 */
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    /**
     * 加载SpringMVC容器配置
     */
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMvcConfig.class);
        return ctx;
    }

    /**
     * 设置哪些请求归属SpringMVC处理
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 加载Spring容器配置
     */
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}

20221225151954


1.3入门案例工作流程分析

20221225153150

1.4Controller加载控制

一、因为功能不同,如何避免Spring错误的加载到SpringMVC的bean?

加载Spring控制的bean的时候排除掉SpringMVC控制的bean

package com.priv.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

/**
 * @author : 十一
 * @data : 14:54 2022/12/25
 * When in doubt, use brute force.
 */
@Configuration
@ComponentScan(value = "com.priv",
        excludeFilters = @ComponentScan.Filter(
            type = FilterType.ANNOTATION,//按注解排除,使用Controller注解排除
            classes = Controller.class                
))
public class SpringConfig {
}
package com.priv.config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

/**
 * @author : 十一
 * @data : 14:56 2022/12/25
 * When in doubt, use brute force.
 */
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    /**
     * 加载SpringMVC容器配置
     */
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMvcConfig.class);
        return ctx;
    }

    /**
     * 设置哪些请求归属SpringMVC处理
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 加载Spring容器配置
     */
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringConfig.class);
        return ctx;
    }
}

二、@ComponentScan

类型:类注解

属性:

三、替换简单的初始化web容器方法:

去继承AbstractDispatcherServletInitializer的子类AbstractAnnotationConfigDispatcherServletInitializer:

package com.priv.config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

/**
 * @author : 十一
 * @data : 14:56 2022/12/25
 * When in doubt, use brute force.
 */
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

1.5PostMan

20221225172844


第二章 请求与响应


2.1请求映射路径

一、团队多人开发,每个人设置不同的请求路径,冲突问题如何解决?

设置模块名作为请求路径前缀

package com.priv.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author : 十一
 * @data : 12:10 2022/12/26
 * When in doubt, use brute force.
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @RequestMapping("/save")
    @ResponseBody
    public String save(){
        System.out.println("book save ...");
        return "{'module' : 'springmvc'}";
    }
}
package com.priv.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author : 十一
 * @data : 17:10 2022/12/25
 * When in doubt, use brute force.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/save")
    @ResponseBody
    public String save(){
        System.out.println("user save ...");
        return "{'module' : 'springmvc'}";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(){
        System.out.println("user delete ...");
        return "{'module' : 'springmvc'}";
    }
}

20221226121942


2.2请求参数

Get请求传参

20221226184238

Post请求参数

20221226184446
package com.priv.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author : 十一
 * @data : 18:05 2022/12/26
 * When in doubt, use brute force.
 */
@Controller
public class UserController {

    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(String name, int age){
        System.out.println("普通参数传递 name ==> " + name);
        System.out.println("普通参数传递 age ==> " + age);
        return "'module' : 'common param'";
    }
}

乱码问题(Post请求)

package com.priv.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

/**
 * @author : 十一
 * @data : 18:06 2022/12/26
 * When in doubt, use brute force.
 */
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[0];
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 乱码处理
     */
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{filter};
    }
}

20221226185112

请求参数

package com.priv.controller;

import com.priv.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * @author : 十一
 * @data : 18:05 2022/12/26
 * When in doubt, use brute force.
 */
@Controller
public class UserController {

    @RequestMapping("/commonParam")
    @ResponseBody
    public String commonParam(String name, int age){
        System.out.println("普通参数传递 name ==> " + name);
        System.out.println("普通参数传递 age ==> " + age);
        return "'module' : 'common param'";
    }

    /**
     * 普通参数:请求参数名与形参名不同
     */
    @RequestMapping("/commonParamDifferentName")
    @ResponseBody
    public String commonParamDifferentName(@RequestParam("name") String username, int age){
        System.out.println("普通参数传递 username ==> " + username);
        System.out.println("普通参数传递 age ==> " + age);
        return "'module' : 'common param different name'";
    }

    /**
     * POJO参数
     */
    @RequestMapping("/pojoParam")
    @ResponseBody
    public String pojoParam(User user){
        System.out.println("pojo参数传递 user ==> " + user);
        return "'module' : 'pojo param'";
    }

    /**
     * 嵌套POJO参数
     */
    @RequestMapping("/pojoContainPojoParam")
    @ResponseBody
    public String pojoContainPojoParam(User user){
        System.out.println("pojo嵌套pojo参数传递 user ==> " + user);
        return "'module' : 'pojo contain pojo param'";
    }

    /**
     * 数组参数
     */
    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes){
        System.out.println("数组参数传递 likes ==> " + Arrays.toString(likes));
        return "'module' : 'array param'";
    }

    /**
     * 集合参数
     */
    @RequestMapping("/listParam")
    @ResponseBody
    public String listParam(@RequestParam List<String> likes){
        System.out.println("集合参数传递 likes ==> " + likes);
        return "'module' : 'list param'";
    }
}
20221227142138

20221227141117

传递json数据

一、json数组

/**
 * 集合参数:json数据
 */
@RequestMapping("/listParamForJson")
@ResponseBody
public String listParamForJson(@RequestBody List<String> likes){
    System.out.println("list common(json)参数传递 list ==> " + likes);
    return "'module' : 'list common for json param'";
}

二、json对象(POJO)

json数据与形参对象属性名相同,定义POJO类型形参即可接收参数

/**
 * POJO参数:json数据
 */
@RequestMapping("/pojoParamForJson")
@ResponseBody
public String pojoParamForJson(@RequestBody User user){
    System.out.println("pojo(json)参数传递 user ==> " + user);
    return "'module' : 'pojo for json param'";
}

三、json数组(POJO)

json数组数据与集合泛型属性名相同,定义List类型形参即可接收参数

/**
 * 集合参数:json数据
 */
@RequestMapping("/listPojoParamForJson")
@ResponseBody
public String listPojoParamForJson(@RequestBody List<User> list){
    System.out.println("list pojo(json)参数传递 list ==> " + list);
    return "'module' : 'list pojo for json param'";
}

第一步:添加json数据转换相关坐标

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.3</version>
</dependency>

第二步:设置发送json数据

20221227144220

第三步:开启自动转换json数据的支持

package com.priv.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
 * @author : 十一
 * @data : 18:04 2022/12/26
 * When in doubt, use brute force.
 */
@Configuration
@ComponentScan("com.priv.controller")
@EnableWebMvc//开启自动转换json数据的支持
public class SpringMvcConfig {
}

第四步:设置接收json数据

@RequestBody



2.3日期类型参数传递

/**
 * 日期参数
 */
@RequestMapping("/date")
@ResponseBody
public String dateParam(Date date,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                        @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date date2){
    System.out.println("参数传递 date ==> " + date);
    System.out.println("参数传递 date1(yyyy-MM-dd) ==> " + date1);
    System.out.println("参数传递 date2(yyyy/MM/dd HH:mm:ss) ==> " + date2);
    return "'module' : 'date param'";
}

类型转换器

public interface Converter<S, T>{
	@Nullable
	T convert(S source);
}
20221227150121

2.4响应json数据

package com.priv.controller;

import com.priv.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : 十一
 * @data : 15:12 2022/12/27
 * When in doubt, use brute force.
 */
@Controller
public class UserController {

    /**
     * 响应页面 跳转页面
     */
    @RequestMapping("/toJumpPage")
    public String toJumpPage(){
        System.out.println("跳转页面");
        return "index.jsp";
    }

    /**
     * 响应文本数据
     */
    @RequestMapping("/toText")
    @ResponseBody
    public String toText(){
        System.out.println("返回纯文本数据");
        return "response text";
    }

    /**
     * 响应POJO对象
     */
    @RequestMapping("/toJsonPOJO")
    @ResponseBody
    public User toJsonPOJO(){
        System.out.println("返回json对象数据");
        User user = new User();
        user.setName("Jack");
        user.setAge(19);
        return user;
    }

    /**
     * 响应POJO集合对象
     */
    @RequestMapping("/toJsonList")
    @ResponseBody
    public List<User> toJsonList(){
        System.out.println("返回json集合数据");
        User user1 = new User();
        user1.setName("Java");
        user1.setAge(14);

        User user2 = new User();
        user2.setName("Go");
        user2.setAge(10);

        ArrayList<User> userList = new ArrayList<>();
        userList.add(user1);
        userList.add(user2);

        return userList;
    }
}

响应

底层:HttpMessageConverter接口

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.http.converter;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;

public interface HttpMessageConverter<T> {
    boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);

    boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType);

    List<MediaType> getSupportedMediaTypes();

    default List<MediaType> getSupportedMediaTypes(Class<?> clazz) {
        return !this.canRead(clazz, (MediaType)null) && !this.canWrite(clazz, (MediaType)null) ? Collections.emptyList() : this.getSupportedMediaTypes();
    }

    T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException;

    void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException;
}

第三章 REST风格


3.1REST简介

REST(Representational State Transfer),表现形式状态转换

​ http://localhost/user/getById?id=1

​ http://localhost/user/saveUser

​ http://localhost/user/1

​ http://localhost/user

优点:

20221227200855

tips:上述行为是约定方式,约定不是规范,可以打破,所以称为REST风格,而不是REST规范

描述模块的名称通常使用复数,也就是加s的格式描述,表示此类资源,而非单个资源,例如:users、books、accounts


3.2RESTful入门案例

package com.priv.controller;

import com.priv.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author : 十一
 * @data : 20:19 2022/12/27
 * When in doubt, use brute force.
 */
@Controller
public class UserController {

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    @ResponseBody
    public String save(){
        System.out.println("user save...");
        return "{'module' : 'user save'}";
    }

    @RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@PathVariable Integer id){
        System.out.println("user delete..." + id);
        return "{'module' : 'user delete'}";
    }

    @RequestMapping(value = "/users", method = RequestMethod.PUT)
    @ResponseBody
    public String update(@RequestBody User user){
        System.out.println("user update..." + user);
        return "{'module' : 'user update'}";
    }

    @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String getById(@PathVariable Integer id){
        System.out.println("user getById..." + id);
        return "{'module' : 'user getById'}";
    }

    @RequestMapping(value = "/users", method = RequestMethod.GET)
    @ResponseBody
    public String getAll(){
        System.out.println("user getAll...");
        return "{'module' : 'user getAll'}";
    }

}

3.3REST快速开发

package com.priv.controller;

import com.priv.domain.User;
import org.springframework.web.bind.annotation.*;

/**
 * @author : 十一
 * @data : 20:46 2022/12/27
 * When in doubt, use brute force.
 */
@RestController
@RequestMapping("/books")
public class BookController {

    @PostMapping
    public String save(){
        System.out.println("book save...");
        return "{'module' : 'book save'}";
    }

    @DeleteMapping("/{id}")
    public String delete(@PathVariable Integer id){
        System.out.println("book delete..." + id);
        return "{'module' : 'book delete'}";
    }

    @PutMapping
    public String update(@RequestBody User user){
        System.out.println("book update..." + user);
        return "{'module' : 'book update'}";
    }

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("book getById..." + id);
        return "{'module' : 'book getById'}";
    }

    @GetMapping
    public String getAll(){
        System.out.println("book getAll...");
        return "{'module' : 'book getAll'}";
    }
}

tips:当访问静态资源时,SpringMVC会进行拦截,需要配置资源放行,交给Tomcat去处理

package com.priv.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author : 十一
 * @data : 21:03 2022/12/27
 * When in doubt, use brute force.
 */
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages/**","/pages/");
        registry.addResourceHandler("/js/**","/js/");
        registry.addResourceHandler("/css/**","/css/");
        registry.addResourceHandler("/plugins/**","/plugins/");
    }
}

第四章 SSM整合


4.1SSM整合

SSM整合流程

1、创建工程

2、SSM整合

3、功能模块

Spring整合MyBatis

Spring整合SpringMVC


4.2表现层数据封装

前端接收数据格式:

public class Result{
	private Object data;
	private Integer code;
	private String msg;
}

tips:Result类中的字段并不是固定的,可以根据需要自行增减,提供若干个构造方法,方便操作

package com.priv.controller;

/**
 * @author : 十一
 * @data : 17:56 2022/12/28
 * When in doubt, use brute force.
 */
public class Code {
    public static final Integer SAVE_OK = 20011;
    public static final Integer DELETE_OK = 20021;
    public static final Integer UPDATE_OK = 20031;
    public static final Integer GET_OK = 20041;

    public static final Integer SAVE_ERR = 20010;
    public static final Integer DELETE_ERR = 20020;
    public static final Integer UPDATE_ERR = 20030;
    public static final Integer GET_ERR = 20040;
}
package com.priv.controller;

import com.priv.domain.Book;
import com.priv.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : 十一
 * @data : 13:06 2022/12/28
 * When in doubt, use brute force.
 */
@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @PostMapping
    public Result save(@RequestBody Book book) {
        boolean flag = bookService.save(book);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @PutMapping
    public Result update(@RequestBody Book book) {
        boolean flag = bookService.update(book);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = bookService.delete(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Book book = bookService.findById(id);
        Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
        String msg = book != null ? "" : "数据查询失败,请重试!";
        return new Result(code, book, msg);
    }

    @GetMapping
    public Result findAll() {
        List<Book> bookList = bookService.findAll();
        Integer code = bookList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = bookList != null ? "" : "数据查询失败,请重试!";
        return new Result(code, bookList, msg);
    }
}

4.3异常处理器

出现异常现象的常见位置与常见诱因如下:

一、各个层级均出现异常,异常处理代码书写在哪一层?

所有的异常均抛出到表现层进行处理

二、表现层处理异常,每个方法单独书写,代码书写量巨大且意义不强,如何解决?

AOP思想


异常处理器

package com.priv.controller;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author : 十一
 * @data : 18:39 2022/12/28
 * When in doubt, use brute force.
 */
@RestControllerAdvice
public class ProjectExceptionAdvice {
    @ExceptionHandler(Exception.class)
    public Result doException(Exception exception){
        System.out.println("出现异常");
        return new Result(666, null);
    }
}

4.4项目异常处理方案

自定义项目系统级异常

package com.priv.exception;

/**
 * @author : 十一
 * @data : 21:13 2022/12/28
 * When in doubt, use brute force.
 */
public class SystemException extends RuntimeException{

    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    /**
     * Constructs a new runtime exception with the specified detail message.
     * The cause is not initialized, and may subsequently be initialized by a
     * call to {@link #initCause}.
     *
     * @param message the detail message. The detail message is saved for
     *                later retrieval by the {@link #getMessage()} method.
     */
    public SystemException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    /**
     * Constructs a new runtime exception with the specified detail message and
     * cause.  <p>Note that the detail message associated with
     * {@code cause} is <i>not</i> automatically incorporated in
     * this runtime exception's detail message.
     *
     * @param message the detail message (which is saved for later retrieval
     *                by the {@link #getMessage()} method).
     * @param cause   the cause (which is saved for later retrieval by the
     *                {@link #getCause()} method).  (A {@code null} value is
     *                permitted, and indicates that the cause is nonexistent or
     *                unknown.)
     * @since 1.4
     */
    public SystemException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

}

自定义项目业务级异常

package com.priv.exception;

/**
 * @author : 十一
 * @data : 21:16 2022/12/28
 * When in doubt, use brute force.
 */
public class BusinessException extends RuntimeException{
    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    /**
     * Constructs a new runtime exception with the specified detail message.
     * The cause is not initialized, and may subsequently be initialized by a
     * call to {@link #initCause}.
     *
     * @param message the detail message. The detail message is saved for
     *                later retrieval by the {@link #getMessage()} method.
     */
    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    /**
     * Constructs a new runtime exception with the specified detail message and
     * cause.  <p>Note that the detail message associated with
     * {@code cause} is <i>not</i> automatically incorporated in
     * this runtime exception's detail message.
     *
     * @param message the detail message (which is saved for later retrieval
     *                by the {@link #getMessage()} method).
     * @param cause   the cause (which is saved for later retrieval by the
     *                {@link #getCause()} method).  (A {@code null} value is
     *                permitted, and indicates that the cause is nonexistent or
     *                unknown.)
     * @since 1.4
     */
    public BusinessException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }
}

自定义异常编码

package com.priv.controller;

/**
 * @author : 十一
 * @data : 17:56 2022/12/28
 * When in doubt, use brute force.
 */
public class Code {
    public static final Integer SAVE_OK = 20011;
    public static final Integer DELETE_OK = 20021;
    public static final Integer UPDATE_OK = 20031;
    public static final Integer GET_OK = 20041;

    public static final Integer SAVE_ERR = 20010;
    public static final Integer DELETE_ERR = 20020;
    public static final Integer UPDATE_ERR = 20030;
    public static final Integer GET_ERR = 20040;

    public static final Integer SYSTEM_ERR = 50001;
    public static final Integer SYSTEM_TIMEOUT_ERR = 50002;
    public static final Integer SYSTEM_UNKNOW_ERR = 59999;
    public static final Integer BUSINESS_ERR = 60002;
}

触发自定义异常

package com.priv.service.impl;

import com.priv.controller.Code;
import com.priv.dao.BookDao;
import com.priv.domain.Book;
import com.priv.exception.BusinessException;
import com.priv.exception.SystemException;
import com.priv.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : 十一
 * @data : 13:05 2022/12/28
 * When in doubt, use brute force.
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;
    /**
     * 按id查询
     *
     * @param id
     * @return
     */
    public Book findById(Integer id) {
        if (id == 1){
            throw new BusinessException(Code.BUSINESS_ERR,"出异常了!");
        }
        try {
            int i = 1/0;
        }catch (Exception e){
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器访问超时,请重试!",e);
        }

        return bookDao.findById(id);
    }
}

拦截并处理异常

package com.priv.controller;

import com.priv.exception.BusinessException;
import com.priv.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author : 十一
 * @data : 18:39 2022/12/28
 * When in doubt, use brute force.
 */
@RestControllerAdvice
public class ProjectExceptionAdvice {

    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException exception){
        /**
         * 记录日志
         * 发送消息给运维
         * 发送邮件给开发人员,exception对象发送给开发人员
         */
        return new Result(exception.getCode(), null, exception.getMessage());
    }


    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException exception){
        return new Result(exception.getCode(), null, exception.getMessage());
    }


    @ExceptionHandler(Exception.class)
    public Result doException(Exception exception){
        /**
         * 记录日志
         * 发送消息给运维
         * 发送邮件给开发人员,exception对象发送给开发人员
         */
        return new Result(Code.SYSTEM_UNKNOW_ERR, null, "系统繁忙,请稍后再试!");
    }
}

4.5案例:SSM整合标准开发

methods: {
    //列表
    getAll() {
        axios.get("/books").then((res)=>{
            this.dataList = res.data.data;
        });
    },

    //弹出添加窗口
    handleCreate() {
        this.dialogFormVisible = true;
        this.resetForm();
    },

    //重置表单
    resetForm() {
        this.formData = {};
    },

    //添加
    handleAdd () {
        axios.post("/books",this.formData).then((res)=>{
            if (res.data.code == 20011){
                this.dialogFormVisible = false;
                this.$message.success("添加成功");
            }else if (res.data.code == 20010){
                this.$message.error("添加失败");
            }else {
                this.$message.error(res.data.msg);
            }
        }).finally(()=>{
           this.getAll();
        });
    },

    //弹出编辑窗口
    handleUpdate(row) {
        axios.get("/books/" + row.id).then((res)=>{
            if (res.data.code == 20041){
                this.formData = res.data.data;
                this.dialogFormVisible4Edit = true;
            }else {
                this.$message.error(res.data.msg);
            }

        });
    },

    //编辑
    handleEdit() {
        axios.put("/books",this.formData).then((res)=>{
            if (res.data.code == 20031){
                this.dialogFormVisible4Edit = false;
                this.$message.success("修改成功");
            }else if (res.data.code == 20030){
                this.$message.error("修改失败");
            }else {
                this.$message.error(res.data.msg);
            }
        }).finally(()=>{
            this.getAll();
        });
    },

    // 删除
    handleDelete(row) {
        this.$confirm("此操作永久删除当前数据,是否继续?", "提示", {
            type: 'info'
        }).then(()=>{
            axios.delete("/books/" + row.id).then((res)=>{
                if (res.data.code == 20021){
                    this.$message.success("删除成功");
                }else {
                    this.$message.error("删除失败");
                }
            }).finally(()=>{
                this.getAll();
            });
        }).catch(()=>{
            this.$message.info("取消删除操作");
        });
    }
}

第五章 拦截器


5.1拦截器概念

20221228223713

拦截器(Interceptor):是一种动态拦截方法调用的机制,在SpringMVC中动态拦截控制器方法的执行

作用:

拦截器与过滤器的区别


5.2入门案例

package com.priv.controller.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author : 十一
 * @data : 22:54 2022/12/28
 * When in doubt, use brute force.
 */
@Component
public class ProjectInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle...");
        /**
         * 是否终止原始操作
         * false:终止
         * true:不终止
         */
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle...");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion...");
    }
}
package com.priv.config;

import com.priv.controller.interceptor.ProjectInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author : 十一
 * @data : 21:03 2022/12/27
 * When in doubt, use brute force.
 */
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {

    @Autowired
    private ProjectInterceptor projectInterceptor;

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(projectInterceptor).addPathPatterns("/books", "/books/*");
    }
}
package com.priv.config;

import com.priv.controller.interceptor.ProjectInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author : 十一
 * @data : 22:51 2022/12/28
 * When in doubt, use brute force.
 */
@Configuration
@ComponentScan("com.priv.controller")
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
    @Autowired
    private ProjectInterceptor projectInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(projectInterceptor).addPathPatterns("/books", "/books/*");
    }
}

执行流程

20221228231051

5.3拦截器参数

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    System.out.println("preHandle...");
    /**
     * 是否终止原始操作
     * false:终止
     * true:不终止
     */
    return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    System.out.println("postHandle...");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    System.out.println("afterCompletion...");
}

5.4拦截器工作流程分析

多拦截器执行顺序

20221228232349

拦截器链的运行顺序


小结

相关源码请查看gitee仓库

#SpringMVC#java#SSM