我正在尝试访问localhost:8080/tree-model-app/ping
URL,该URL应该返回true
。但我得到该URL的404
。
这是我的main
应用程序的Spring Boot
@SpringBootApplication
public class TreeModelApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(TreeModelApplication.class, args);
}
@Bean
public ServletRegistrationBean servletRegistrationBean() throws Exception {
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new RootServlet(), "/tree-model-app");
registrationBean.setLoadOnStartup(1);
registrationBean.setAsyncSupported(true);
return registrationBean;
}
}
这是我的servlet
public class RootServlet extends ServletHttpHandlerAdapter {
public RootServlet(HttpHandler httpHandler) {
super(httpHandler);
}
public RootServlet() {
this(WebHttpHandlerBuilder
.webHandler(toHttpHandler(routingFunction()))
.build()
);
}
private static RouterFunction<?> routingFunction() {
return route(GET("/ping"), new PingHandlerFunction());
}
}
这是我的handler
public class PingHandlerFunction implements HandlerFunction<ServerResponse> {
@Override
public Mono<ServerResponse> handle(ServerRequest request) {
return ok().body(fromObject("true"));
}
}
这是我的build.gradle
buildscript {
repositories {
jcenter()
maven { url 'https://repo.spring.io/libs-snapshot' }
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
}
}
apply plugin: 'war'
apply plugin: 'org.springframework.boot'
repositories {
jcenter()
maven { url "http://repo.spring.io/snapshot" }
maven { url "http://repo.spring.io/milestone" }
}
group = 'com.lapots.tree.model.web'
dependencies {
compile "org.springframework.boot:spring-boot-starter-parent:$springBootVersion"
compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
compile "org.springframework.boot:spring-boot-starter-webflux:$springBootVersion"
compile "org.springframework.boot:spring-boot-starter-jetty:$springBootVersion"
compile "org.springframework.boot:spring-boot-starter-actuator:$springBootVersion"
}
但是当我尝试访问localhost:8080/tree-model-app/ping
时,我得到了问题是什么?
最佳答案
作为解决方案,我喜欢这样。我不知道为什么在/tree-model-app
初始化期间映射/tree-model-app/*
或ServletRegistrationBean
不起作用,但是我在这里将其设置为/*
。
ServletRegistrationBean<Servlet> registrationBean =
new ServletRegistrationBean<>(new RootServlet(), "/*");
接下来,我直接在路由器功能中设置所需的路径
private static RouterFunction<?> routingFunction() {
return route(GET("/tree-model-app/ping"), new PingHandlerFunction());
}
它开始工作
localhost:8080/tree-model-app/ping
关于java - 当我尝试在Spring Boot应用程序中访问URL时找不到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43895308/