springBoot学习(五)springBoot自定义banner与web开发

时间:2022-06-24
本文章向大家介绍springBoot学习(五)springBoot自定义banner与web开发,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

@SpringBootApplication

该注解相关的几个方法

  • exclude,排除某些类添加至spring容器管理,参数为class对象
  • excludeName,排除某些类添加至spring容器管理,参数为class path字符串
  • scanBasePackages,将某些包下的类添加进入spring扫描文件,参数为class path字符串
  • scanBasePackageClasses,将某些包下的类添加进入spring扫描文件,参数为class对象

springBoot的banner图

不显示banner

通过 springBootTestRun.setBannerMode(Banner.Mode.OFF);

package com.yxj.spring;

import com.yxj.spring.Initializer.MyApplicationContextInitializer;
import com.yxj.spring.monitor.MyApplicationListener;
import com.yxj.spring.monitor.MyEvent;
import com.yxj.spring.properties.TestProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;

import java.util.List;

/**
 * @ProjectName: springBootDemo
 * @Package: com.yxj.spring
 * @Description:
 * @Author: 阿杰
 * @CreateDate: 2019/1/18 20:18
 * @UpdateUser: 暂无
 * @UpdateDate: 2019/1/18 20:18
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@SpringBootApplication
public class SpringBootTestRun {

    public static void main(String[] args) {
        SpringApplication springBootTestRun = new SpringApplication(SpringBootTestRun.class);
        springBootTestRun.setBannerMode(Banner.Mode.OFF);
        ConfigurableApplicationContext run = springBootTestRun.run(args);
        run.close();
    }
}

自定义banner

方式一

在resource目录下新建banner.txt,txt放入自定义内容

方式二

在resource目录下新建自定义名称.txt,在application.properties中添加“spring.banner.location=自定义名称.txt”

方式三

自定义banner也可以是图片,会自动解析为ASCII艺术字,名称为banner.jpg/banner.png/banner.gif

方式四

自定义图片名称,通过在application.properties中添加“spring.banner.image.location=自定义名称.jpg”

在启动项目的时候,文字和图片可以共同显示,图片优先级大于文字
SpringBoot自定义启动Banner在线生成工具

https://www.bootschool.net/ascii

SpringBoot艺术字

https://www.bootschool.net/ascii-art

web开发

测试访问

pom文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yxj.spring</groupId>
    <artifactId>springBootDemo</artifactId>
    <version>1.0.0</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

</project>

controller方法

package com.yxj.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ProjectName: springBootDemo
 * @Package: com.yxj.spring.controller
 * @Description: @RestController表示当前controller方法的返回值可以直接用于返回值输出==@ResponseBody
 * @Author: 阿杰
 * @CreateDate: 2019/1/27 23:21
 * @UpdateUser: 暂无
 * @UpdateDate: 2019/1/27 23:21
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@RestController
@RequestMapping("my")
public class MyController {

    @GetMapping("testWeb")
    public String testWeb(HttpServletRequest request){
        return "测试";
    }

    @GetMapping(value = "/pathParam/{id}")
    public String pathParam(@PathVariable(value = "id") String id){
        return "测试, id = "+id;
    }
}

springBoot启动类

package com.yxj.spring;

import com.yxj.spring.Initializer.MyApplicationContextInitializer;
import com.yxj.spring.monitor.MyApplicationListener;
import com.yxj.spring.monitor.MyEvent;
import com.yxj.spring.properties.TestProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;

import java.util.List;

/**
 * @ProjectName: springBootDemo
 * @Package: com.yxj.spring
 * @Description:
 * @Author: 阿杰
 * @CreateDate: 2019/1/18 20:18
 * @UpdateUser: 暂无
 * @UpdateDate: 2019/1/18 20:18
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@SpringBootApplication
public class SpringBootTestRun {

    public static void main(String[] args) {
        SpringApplication springBootTestRun = new SpringApplication(SpringBootTestRun.class);
        ConfigurableApplicationContext run = springBootTestRun.run(args);
    }
}

测试访问路径 http://localhost:8080/my/testWeb

指定端口

application.properties

server.port=80

返回jsp页面

springBoot默认不支持jsp组建,需要添加maven包 添加依赖包

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

application.properties指定前缀和后缀

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

prefix=/,这里的/指的是webapp,为jsp根目录,idea中可以设置web根目录

webapp下新增test.jsp

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
this is test jsp , get controller param is ${testParam}
</body>
</html>

访问jsp的controller代码

package com.yxj.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ProjectName: springBootDemo
 * @Package: com.yxj.spring.controller
 * @Description:
 * @Author: 阿杰
 * @CreateDate: 2019/1/28 0:00
 * @UpdateUser: 暂无
 * @UpdateDate: 2019/1/28 0:00
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@Controller
public class JspController {

    @GetMapping(value = "/toTsetJsp/{id}")
    public ModelAndView toTsetJsp(@PathVariable String id){
        ModelAndView modelAndView = new ModelAndView("/test");
        modelAndView.addObject("testParam",id);
        return modelAndView;
    }
}

使用freemarker

加入依赖,pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

访问controller

package com.yxj.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ProjectName: springBootDemo
 * @Package: com.yxj.spring.controller
 * @Description:
 * @Author: 阿杰
 * @CreateDate: 2019/1/28 20:47
 * @UpdateUser: 暂无
 * @UpdateDate: 2019/1/28 20:47
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@Controller
public class FreemarkerController {

    @GetMapping(value = "/testFreeMarker/{id}")
    public ModelAndView testFreeMarker(@PathVariable(value = "id") String id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("a");
        mv.addObject("userName",id);
        return mv;
    }
}

在resource下添加文件夹templates,其中添加a.ftl

<h1>this is test ftl, get param is ${userName}</h1>

访问链接 http://localhost/testFreeMarker/123 访问页面,数据渲染正常

默认目录为classpath:/templates/ 源码中有个默认值

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

package org.springframework.boot.autoconfigure.freemarker;

import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.autoconfigure.template.AbstractTemplateViewResolverProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(
    prefix = "spring.freemarker"
)
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {
    public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";
    public static final String DEFAULT_PREFIX = "";
    public static final String DEFAULT_SUFFIX = ".ftl";
    private Map<String, String> settings = new HashMap();
    private String[] templateLoaderPath = new String[]{"classpath:/templates/"};
    private boolean preferFileSystemAccess = true;

    public FreeMarkerProperties() {
        super("", ".ftl");
    }

    public Map<String, String> getSettings() {
        return this.settings;
    }

    public void setSettings(Map<String, String> settings) {
        this.settings = settings;
    }

    public String[] getTemplateLoaderPath() {
        return this.templateLoaderPath;
    }

    public boolean isPreferFileSystemAccess() {
        return this.preferFileSystemAccess;
    }

    public void setPreferFileSystemAccess(boolean preferFileSystemAccess) {
        this.preferFileSystemAccess = preferFileSystemAccess;
    }

    public void setTemplateLoaderPath(String... templateLoaderPaths) {
        this.templateLoaderPath = templateLoaderPaths;
    }
}

通过在application.properties中可以配置默认路径

spring.freemarker.template-loader-path=classpath:/templatesTest/

切换jetty容器

springBoot默认内置的是tomcat容器,切换jetty容器,需要配置 pom文件spring-boot-starter-web排除tomcat的引用,注释掉springBoot支持jsp,该依赖会导致切换jetty成功,但是使用的依然是tomcat容器

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yxj.spring</groupId>
    <artifactId>springBootDemo</artifactId>
    <version>1.0.0</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--排除tomcat的引用-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--使springBoot支持jsp-->
        <!--<dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>-->

        <!--使springBoot支持freemarker-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!--添加jetty依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

</project>

重新编译,查看日志,包含有jetty字样,表示容器切换成功

JettyWebServer  : Jetty started on port(s) 80 (http/1.1) with context path '/'

关于jetty与tomcat区别与用途,可以查看博客https://blog.csdn.net/u014209975/article/details/52598428

静态资源读取

1.在webapp下-web根目录,在文件下面新建img,放入test.png 可以通过http://localhost/img/test.png 正常访问到 2.在resource下的这几个文件,也是可以放静态文件的,默认访问路径也是/

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };

经过测试,以下两个链接可以访问到各个对应的静态图片

  • http://localhost/springBootDemo/img/test1.png
  • http://localhost/springBootDemo/img/test.png

3.可以通过在application.properties中添加参数来指定默认路径

#修改静态资源默认访问路径(修改之后,resource下的几个默认文件地址不会生效,webapp可以正常访问)
spring.resources.static-locations=classpath:/html/

springBoot异常处理

springBoot对异常做了特殊处理
springBoot处理之后-404页面
springBoot处理之后-500页面
排除springBoot对异常处理的方式
package com.yxj.spring;

import com.yxj.spring.Initializer.MyApplicationContextInitializer;
import com.yxj.spring.monitor.MyApplicationListener;
import com.yxj.spring.monitor.MyEvent;
import com.yxj.spring.properties.TestProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;

import java.util.List;

/**
 * @ProjectName: springBootDemo
 * @Package: com.yxj.spring
 * @Description:
 * @Author: 阿杰
 * @CreateDate: 2019/1/18 20:18
 * @UpdateUser: 暂无
 * @UpdateDate: 2019/1/18 20:18
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */

/**
 * 排除springBoot对异常的默认处理
 */
@SpringBootApplication(exclude = ErrorMvcAutoConfiguration.class)
public class SpringBootTestRun {

    public static void main(String[] args) {
        SpringApplication springBootTestRun = new SpringApplication(SpringBootTestRun.class);
        ConfigurableApplicationContext run = springBootTestRun.run(args);
    }
}
springBoot处理之前-500
springBoot处理之前-404
自定义异常处理
1.通过实现ErrorPageRegistrar接口的方式(类似web.xml中配置的errpage标签的形式)
package com.yxj.spring.myException;

import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

/**
 * @ProjectName: springBootDemo
 * @Package: com.yxj.spring.myException
 * @Description:
 * @Author: 阿杰
 * @CreateDate: 2019/1/29 14:51
 * @UpdateUser: 暂无
 * @UpdateDate: 2019/1/29 14:51
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@Component
public class CommonErrorPageRegistrar implements ErrorPageRegistrar {

    /**
     * 如果相对某个异常做单独页面的跳转处理
     * ErrorPage(Class<? extends Throwable> exception, String path)
     * 可以添加一个Class对象,和跳转路径。
     * @param registry
     */
    @Override
    public void registerErrorPages(ErrorPageRegistry registry) {
        ErrorPage ep404 = new ErrorPage(HttpStatus.NOT_FOUND,"/html/404.html");
        ErrorPage ep500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/html/500.html");
        registry.addErrorPages(ep404,ep500);
    }
}
自定义异常处理的页面-404
自定义异常处理的页面-500
2.通过@ExceptionHandler注解(这个处理方式可以返回字符串或者json,可以获取到具体的异常message,通常用作全局异常处理,也可以做细分)
package com.yxj.spring.myException;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ProjectName: springBootDemo
 * @Package: com.yxj.spring.myException
 * @Description:
 * @Author: 阿杰
 * @CreateDate: 2019/1/29 15:18
 * @UpdateUser: 暂无
 * @UpdateDate: 2019/1/29 15:18
 * @UpdateRemark: The modified content
 * @Version: 1.0
 */
@ControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String handleException(Exception e){
        return "检测到异常:"+e.getMessage();
    }
}

tomcat配置

内置tomcat的log access相关配置
#缓冲输出,定期刷新
server.tomcat.accesslog.buffered=true
#创建日志文件的目录。可以是相对于Tomcat基目录或绝对目录。
server.tomcat.accesslog.directory=d:/springBootDemoLogs
#启用日志(true会生成日志文件到directory指定的路径,false不会生成)
server.tomcat.accesslog.enabled=true
#生成的日志名称的日期格式
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd
#访问日志的格式模式 详细使用查看- https://www.cnblogs.com/chrischennx/p/6746214.html
server.tomcat.accesslog.pattern=common
#日志文件名字前缀
server.tomcat.accesslog.prefix=springBoot_demo_logs
#是否延迟在文件名中包含日期戳,直到旋转时间
server.tomcat.accesslog.rename-on-rotate=false
#设置请求的IP地址,主机名,协议和端口的请求属性
server.tomcat.accesslog.request-attributes-enabled=false
#是否启用访问日志轮换。
server.tomcat.accesslog.rotate=true
#日志文件名称的后缀
server.tomcat.accesslog.suffix=.log
tomcat最大线程数和连接数设置
#设置tomcat的最大线程数
server.tomcat.max-threads=1000
#设置tomcat的最大连接数
server.tomcat.max-connections=20000