Home  >  Article  >  Java  >  How to modify the default context of Spring Boot (detailed method)

How to modify the default context of Spring Boot (detailed method)

不言
不言Original
2018-09-25 15:27:012994browse

The content of this article is about how to modify the default context of Spring Boot (detailed explanation of the method). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Preface

By default, the service context used by Spring Boot is "/", we can pass"http://localhost:PORT/" Access the application directly;

But in the production environment, in many cases, we need to change the default context of the application;

Fortunately, Spring Boot is very flexible and simple, and provides functions There are many ways to change the context of the application;

1) Change the context through the application.properties configuration file

Through the application.properties configuration file, we can configure it very easily Various parameters, changing the context is one of them, the configuration is as follows:

### Default server path #########
server.port=8080
### Context root path ########
server.contextPath=/home

2) Change the context by implementing the EmbeddedServletContainerCustomizer interface

EmbeddedServletContainerCustomizer interface can be used for customization Configure the parameters related to the built-in Servlet container. Any Bean that implements the EmbeddedServletContainerCustomizer interface will get a callback, which will be executed before the container starts. We can set the port, context, error page, etc.;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;
@Component
public class AppContainerCustomizer implements EmbeddedServletContainerCustomizer {
 
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
 
        container.setPort(8080);
        container.setContextPath("/home");
 
    }
}

3) Change the context through the startup command line

If our application is packaged in uber, we can use the following startup command to configure the context:

java -jar -Dserver.contextPath=/home spring-boot-demo.jar

The above is the detailed content of How to modify the default context of Spring Boot (detailed method). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn