Spring Boot Sample 014之spring-boot-error-controller

一、环境

  • Idea 2020.1
  • JDK 1.8
  • maven

二、目的

spring boot 异常处理controller实现方式。
gitHub地址: https://github.com/ouyushan/ouyushan-spring-boot-samples

三、步骤

3.1、点击File -> New Project -> Spring Initializer,点击next

Spring Boot Sample 014之spring-boot-error-controller_第1张图片

 3.2、选择Web依赖,选中Spring Web。可以选择Spring Boot版本,本次默认为2.2.6,点击Next

Spring Boot Sample 014之spring-boot-error-controller_第2张图片

3.3、项目结构

Spring Boot Sample 014之spring-boot-error-controller_第3张图片

 四、添加文件

Spring Boot Sample 014之spring-boot-error-controller_第4张图片

 

pom.xml文件
xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.6.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>org.ouyushangroupId>
    <artifactId>spring-boot-error-controllerartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>spring-boot-error-controllername>
    <description>Error Controller project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-freemarkerartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.webjarsgroupId>
            <artifactId>jqueryartifactId>
            <version>3.5.0version>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

 

添加index.ftl
DOCTYPE html>
<html>
<head lang="en">
    <title>Spring Boot Demo - FreeMarkertitle>

    <link href="/css/index.css" rel="stylesheet" />

head>
<body>
<div style="text-align: center;">
    <img src="/images/springboot.jpg" />
    <h1 id="title">${title}h1>
div>

<script type="text/javascript" src="/webjars/jquery/3.5.0/jquery.min.js">script>

<script>
    $(function(){
        $('#title').click(function(){
            alert('点击了');
        });
    })
script>
body>
html>

 

添加index.css
h1{color: blue;}
 
   
配置文件application.properties
spring.freemarker.template-loader-path=classpath:/templates
spring.freemarker.suffix=.ftl
 
   
BaseErrorController.java
package org.ouyushan.springboot.error.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Description:
 * @Author: ouyushan
 * @Email: [email protected]
 * @Date: 2020/5/7 13:51
 */

@Controller
@RequestMapping(value = "error")
public class BaseErrorController implements ErrorController {


    private static final Logger logger = LoggerFactory.getLogger(BaseErrorController.class);

    @Override
    public String getErrorPath() {
        logger.info("出错啦!进入自定义错误控制器");
        return "error/error";
    }

    @RequestMapping
    public String error() {
        return getErrorPath();
    }
}
 
   
UserController .java
package org.ouyushan.springboot.error.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Description:
 * @Author: ouyushan
 * @Email: [email protected]
 * @Date: 2020/5/7 13:54
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/index")
    public String index(ModelMap map){
        map.put("title", "freemarker hello word");
        return "index"; // 开头不要加上/,linux下面会出错
    }

    @RequestMapping(value = "/error")
    public String error(ModelMap map) {
        throw new RuntimeException("测试异常");
    }
}
 
   

五、测试

http://localhost:8080/error/

你可能感兴趣的:(Spring Boot Sample 014之spring-boot-error-controller)