在Spring Boot中配置web app

本文将会介绍怎么在Spring Boot中创建和配置一个web应用程序。

添加依赖

如果要使用Spring web程序,则需要添加如下依赖:

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

配置端口

正如我们之前文章中提到的,要想配置端口需要在application.properties文件中配置如下:

server.port=8083

如果你是用的是yaml文件,则:

server:
port: 8083

或者通过java文件的形式:

@Component
public class CustomizationBean implements
WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Override
public void customize(ConfigurableServletWebServerFactory container) {
container.setPort(8083);
}
}

配置Context Path

默认情况下,Spring MVC的context path是‘/’, 如果你想修改,那么可以在配置文件application.properties中修改:

server.servlet.contextPath=/springbootapp

如果是yaml文件:

server:
servlet:
contextPath:/springbootapp

同样的,可以在java代码中修改:

@Component
public class CustomizationBean
implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Override
public void customize(ConfigurableServletWebServerFactorycontainer) {
container.setContextPath("/springbootapp");
}
}

配置错误页面

默认情况下Spring Boot会开启一个whitelabel的功能来处理错误,这个功能本质上是自动注册一个BasicErrorController如果你没有指定错误处理器的话。同样的,这个错误控制器也可以自定义:

@RestController
public class MyCustomErrorController implements ErrorController { private static final String PATH = "/error"; @GetMapping(value=PATH)
public String error() {
return "Error haven";
} @Override
public String getErrorPath() {
return PATH;
}
}

当然,和之前讲过的自定义服务器信息一样,你也可以自定义错误页面,就像在web.xml里面添加error-page:

@Component
public class CustomizationBean
implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Override
public void customize(ConfigurableServletWebServerFactorycontainer) {
container.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
container.addErrorPages(new ErrorPage("/errorHaven"));
}
}

通过这个功能,你可以对错误进行更加细致的分类。

在程序中停止Spring Boot

SpringApplication提供了一个静态的exit()方法,可以通过它来关停一个Spring Boot应用程序:

    @Autowired
public void shutDown(ApplicationContext applicationContext) {
SpringApplication.exit(applicationContext, new ExitCodeGenerator() {
@Override
public int getExitCode() {
return 0;
}
});
}

第二个参数是一个ExitCodeGenerator的实现,主要用来返回ExitCode。

配置日志级别

我们可以在配置文件中这样配置日志级别:

logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR

注册Servlet

有时候我们需要将程序运行在非嵌套的服务器中,这时候有可能会需要自定义servlet的情况,Spring Boot 也提供了非常棒的支持,我们只需要在ServletRegistrationBean中,注册servlet即可:

    @Bean
public ServletRegistrationBean servletRegistrationBean() { ServletRegistrationBean bean = new ServletRegistrationBean(
new SpringHelloWorldServlet(), "/springHelloWorld/*");
bean.setLoadOnStartup(1);
bean.addInitParameter("message", "SpringHelloWorldServlet special message");
return bean;
}

切换嵌套服务器

默认情况下,Spring Boot会使用tomcat作为嵌套的内部服务器,如果想切换成jetty则可以这样:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>

exclude自带的Tomcat,并额外添加spring-boot-starter-jetty即可。

本文的例子可参考: https://github.com/ddean2009/learn-springboot2/tree/master/springboot-config-webapp

更多教程请参考 flydean的博客

在Spring Boot中配置web app的更多相关文章

  1. Spring Boot2 系列教程(八)Spring Boot 中配置 Https

    https 现在已经越来越普及了,特别是做一些小程序或者公众号开发的时候,https 基本上都是刚需了. 不过一个 https 证书还是挺费钱的,个人开发者可以在各个云服务提供商那里申请一个免费的证书 ...

  2. spring boot中配置日志log和热部署

    Java的日志有很多 个人强烈不推荐log4j ,推荐log4j2和logback 在高并发,多线程的环境下log4j1 的性能和log4j2相比可以用junk来形容  对就是junk.log4j2的 ...

  3. spring、spring boot中配置多数据源

    在项目开发的过程中,有时我们有这样的需求,需要去调用别的系统中的数据,那么这个时候系统中就存在多个数据源了,那么我们如何来解决程序在运行的过程中到底是使用的那个数据源呢? 假设我们系统中存在2个数据源 ...

  4. Spring boot中配置HikariCP连接池

    # jdbc_config datasourcespring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasourc ...

  5. Spring Boot 中配置切换

    步骤一:切换需求 有时候在本地测试是使用8080端口(默认端口),可是上线时使用的比如是9090端口(不常用的,以防被黑). 此时就可以通过多配置文件实现多配置支持与灵活切换. 步骤二:多配置文件 3 ...

  6. 二、spring Boot构建的Web应用中,基于MySQL数据库的几种数据库连接方式进行介绍

    包括JDBC.JPA.MyBatis.多数据源和事务. 一.JDBC 连接数据库 1.属性配置文件(application.properties) spring.datasource.url=jdbc ...

  7. Spring Boot中使用JavaMailSender发送邮件

    相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看 ...

  8. Spring Boot 属性配置&自定义属性配置

    在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...

  9. spring boot中的声明式事务管理及编程式事务管理

    这几天在做一个功能,具体的情况是这样的: 项目中原有的几个功能模块中有数据上报的功能,现在需要在这几个功能模块的上报之后生成一条消息记录,然后入库,在写个接口供前台来拉取消息记录. 看到这个需求,首先 ...

随机推荐

  1. 你还不知道Vue的生命周期吗?带你从Vue源码了解Vue2.x的生命周期(初始化阶段)

    作者:小土豆biubiubiu 博客园:https://www.cnblogs.com/HouJiao/ 掘金:https://juejin.im/user/58c61b4361ff4b005d9e8 ...

  2. 初始WebApi(1)

    如果你要问我WebApi是干嘛,我只能说它是的给数据.哈哈哈哈哈,这几天也才刚刚了解了解关于WebApi的知识,今天就来谈谈吧. 1.创建WebApi项目 第一步:选择ASP.NET Web应用程序 ...

  3. 【Selenium01篇】python+selenium实现Web自动化:搭建环境,Selenium原理,定位元素以及浏览器常规操作!

    一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 二.话不多说,直接开干,开始搭建自动化测试环境 这里以前在 ...

  4. easy-mock 本地部署(挤需体验三番钟,里造会干我一样,爱象节款mock)

    前言 很多小伙伴问我怎么在自己公司的项目里面添加配置mock,在vue项目里面都知道怎么配置mock,在大型前端项目里面就一脸疑惑了. 我就回答他,你今天会在vue项目里面用,那天换公司是用angul ...

  5. matplotlib中的基本概念

    有外语基础的朋友看这里: matplotlib官方文档 Figure(图像): 组成部分

  6. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十)之Inner Classes

    The inner class is a valuable feature because it allows you to group classes that logically belong t ...

  7. Spring Cloud 系列之 Gateway 服务网关(二)

    本篇文章为系列文章,未读第一集的同学请猛戳这里:Spring Cloud 系列之 Gateway 服务网关(一) 本篇文章讲解 Gateway 网关的多种路由规则.动态路由规则(配合服务发现的路由规则 ...

  8. G - Number Transformation BFS

    In this problem, you are given an integer number s. You can transform any integer number A to anothe ...

  9. 浏览器中 JS 的事件循环机制

    目录 事件循环机制 宏任务与微任务 实例分析 参考 1.事件循环机制 浏览器执行JS代码大致可以分为三个步骤,而这三个步骤的往复构成了JS的事件循环机制(如图). 第一步:主线程(JS引擎线程)中执行 ...

  10. 通过dockerfile制作镜像

    Dockerfile是一个用于构建Docker镜像的文本文件,其中包含了创建Docker镜像的全部指令.就是将我们安装环境的每个步骤使用指令的形式存放在一个文件中,最后生成一个需要的环境. Docke ...