我正在尝试以编程方式设置Spring Boot应用程序上下文根。上下文根的原因是我们希望应用程序可以从localhost:port/{app_name}访问,并有所有的控制器路径附加到它。

下面是web-app的应用程序配置文件。

@Configuration
public class ApplicationConfiguration {

  Logger logger = LoggerFactory.getLogger(ApplicationConfiguration.class);

  @Value("${mainstay.web.port:12378}")
  private String port;

  @Value("${mainstay.web.context:/mainstay}")
  private String context;

  private Set<ErrorPage> pageHandlers;

  @PostConstruct
  private void init(){
      pageHandlers = new HashSet<ErrorPage>();
      pageHandlers.add(new ErrorPage(HttpStatus.NOT_FOUND,"/notfound.html"));
      pageHandlers.add(new ErrorPage(HttpStatus.FORBIDDEN,"/forbidden.html"));
  }

  @Bean
  public EmbeddedServletContainerFactory servletContainer(){
      TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
      logger.info("Setting custom configuration for Mainstay:");
      logger.info("Setting port to {}",port);
      logger.info("Setting context to {}",context);
      factory.setPort(Integer.valueOf(port));
      factory.setContextPath(context);
      factory.setErrorPages(pageHandlers);
      return factory;
  }

  public String getPort() {
      return port;
  }

  public void setPort(String port) {
      this.port = port;
  }
}

这里是主页面的索引控制器。

@Controller
public class IndexController {

  Logger logger = LoggerFactory.getLogger(IndexController.class);

  @RequestMapping("/")
  public String index(Model model){
      logger.info("Setting index page title to Mainstay - Web");
      model.addAttribute("title","Mainstay - Web");
      return "index";
  }

}

应用程序的新根目录应该位于localhost:12378/主流,但它仍然位于localhost:12378。

是什么原因导致Spring Boot没有在请求映射之前追加上下文根?


当前回答

你可以像下面这样在Spring Boot: 2.1.6中使用:

server.servlet.context-path=/api-path

其他回答

<!-- Server port-->

server.port=8080

<!--Context Path of the Application-->

server.servlet.context-path=/ems

如果您正在使用Spring Boot,那么您不必通过Bean初始化来配置服务器属性。

相反,如果一个功能可用于基本配置,那么它可以在一个名为application的“属性”文件中设置,该文件应该位于应用程序结构的src\main\resources下。“properties”文件有两种格式

.yml . properties

指定或设置配置的方式因格式的不同而不同。

在您的特定情况下,如果您决定使用扩展名.properties,那么您将有一个名为application的文件。src\main\resources下的属性,使用以下配置设置

server.port = 8080
server.contextPath = /context-path

OTOH,如果你决定使用.yml扩展名(即application.yml),你需要使用以下格式(即YAML)设置配置:

server:
    port: 8080
    contextPath: /context-path

有关Spring Boot的更多常见属性,请参阅下面的链接:

https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

您可以将此属性添加到spring属性文件中

spring.data.rest.basePath=/your_path

我们可以使用WebServerFactoryCustomizer设置它。这可以直接添加到spring引导main方法类中,该类会启动spring ApplicationContext。

@Bean
    public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
      webServerFactoryCustomizer() {
        return factory -> factory.setContextPath("/demo");
}

您可以通过添加端口和contextpath轻松地在[src\main\resources] .properties文件和.yml文件中添加配置

应用程序。属性文件配置

server.port = 8084
server.contextPath = /context-path

应用程序。Yml文件配置

server:
port: 8084
contextPath: /context-path

我们还可以在spring引导中以编程方式更改它。

@Component
public class ServerPortCustomizer implements     WebServerFactoryCustomizer<EmbeddedServletContainerCustomizer > {

@Override
public void customize(EmbeddedServletContainerCustomizer factory) {
    factory.setContextPath("/context-path");
    factory.setPort(8084);
}

}

我们还可以用另一种方法

@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {SpringApplication application =     new pringApplication(MyApplication.class);
    Map<String, Object> map = new HashMap<>();
    map.put("server.servlet.context-path", "/context-path");
    map.put("server.port", "808");
    application.setDefaultProperties(map);
    application.run(args);
    }       
}

使用Java命令spring boot。X

java -jar my-app.jar --server.contextPath=/spring-boot-app     --server.port=8585 

使用Java命令spring boot。X

java -jar my-app.jar --server.servlet.context-path=/spring-boot-app --server.port=8585