当前位置:首页 > 技术知识 > 正文内容

在Spring Boot中配置web app(spring boot搭建web项目)

maynowei1周前 (08-12)技术知识12

本文将会介绍怎么在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即可。

欢迎关注我的公众号:程序那些事,更多精彩等着您!

更多内容请访问:flydean的博客 flydean.com


相关文章

验证码,除了 12306,我还没有服过谁

为了防止暴力注册或爬虫爬取等机器请求,需要验证操作者是人还是机器,便有了验证码这个设计。本文作者主要介绍了如何使用 Axure 来设计一个动态的图形验证码,一起来学习一下吧。在软件设计中,为了防止暴力...

惊现!iOS 16.5 kfd 漏洞利用,成功隐藏 Dock 栏

最近!kfd漏洞比较活跃,进展也是很顺利,今天就有大神成功使用 kfd 漏洞实现隐藏 Dock 栏,到底怎么回事?请继续往下看。-- kfd 漏洞说明 --kfd漏洞适合在 iOS 16.2 - 16...

微软明年要停止SQL Server 2005的技术支持了

站长之家(Chinaz.com)12月28日消息据外媒消息称,微软将于明年停止为SQL Server 2005提供技术支持,即不再为其提供新的安全补丁、新功能、应用升级等服务。且表示在停止技术支持后,...

Flutter 之 ListView(flutter框架)

在 Flutter 中,ListView 可以沿一个方向(垂直或水平方向)来排列其所有子 Widget,常被用于需要展示一组连续视图元素的场景ListView 构造方法ListView:仅适用于列表中...

ExpandListView 的一种巧妙写法(三十的另一种写法)

ExpandListView大家估计也用的不少了,一般有需要展开的需求的时候,大家不约而同的都想到了它然后以前自己留过记录的一般都会找找以前自己的代码,没有记录习惯的就会百度、谷歌,这里吐槽一下,好几...

Win10桌面/手机版最深层次开发功能挖掘

IT之家讯 Win10开发者预览版为我们提供了一个Win10大框架的早期概览,使开发者与热心用户都可以提前感受Win10带来的新特性,尝试新工具,而作为开发者,最关心的莫过于Windows多平台通用应...