Spring Boot - Как изменить контекстный путь

Spring Boot - Как изменить контекстный путь

В Spring Boot, чтобы изменить путь контекста, обновите свойстваserver.contextPath. Следующие примеры обновляют путь контекста с/ до/example илиhttp://localhost:8080/example.

Note
По умолчанию контекстный путь - «/».

P.S Tested with Spring Boot 1.4.2.RELEASE

1. Свойства & Ямл

1.1 Update via a properties file.

/src/main/resources/application.properties

server.port=8080
server.contextPath=/example

1.2 Update via a yaml file.

/src/main/resources/application.yml

server:
  port: 8080
  contextPath: /example

2. EmbeddedServletContainerCustomizer

Обновление через код, это переопределяет свойства и настройки yaml.

CustomContainer.java

package com.example;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.stereotype.Component;

@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {

        container.setPort(8080);
        container.setContextPath("/example");

    }

}

3. Командная строка

Обновите контекстный путь, передавая системные свойства напрямую.

Терминал

java -jar -Dserver.contextPath=/example spring-boot-example-1.0.jar