java - 当我尝试在Spring Boot应用程序中访问URL时找不到页面

标签 java spring gradle spring-boot jetty

我正在尝试访问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时,我得到了
enter image description here
问题是什么?

最佳答案

作为解决方案,我喜欢这样。我不知道为什么在/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/

相关文章:

java - 在 Spring Boot 应用程序中禁用 Spring Security

android - "The signing configuration should be specified in Gradle build scripts"...我做到了

java - 无法在 Android Studio (libGDX) 中运行项目

java - 多模块 Gradle 构建问题 - 找不到与给定名称匹配的资源

java - 关于标准 JOptionPane 上的取消按钮的问题

java - 我想修复屏幕上 Java 程序启动的位置

java - Eclipse 无法识别 com.sun.net.httpserver.HttpServer 包

spring - 使用 @PropertySource 注释时未解析 @Value。如何配置 PropertySourcesPlaceholderConfigurer?

spring - Spring中的JUnit测试-覆盖和忽略来自应用程序其他配置类的bean

java - 从哪里给我的类(class)打电话