我正在尝试以编程方式设置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没有在请求映射之前追加上下文根?
您可以通过添加端口和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
您可以通过添加端口和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