Tomcat 的简单 JAX-RS - 404 未找到(无 web.xml)

Simple JAX-RS with Tomcat - 404 Not Found (No web.xml)

服务

    @Path("/rest")
    public class JustService {

        @GET
        @Produces("text/plain")
        @Path("sayhi")
        public String sayhi(){
            return "Hi";
        }
     }

应用程序配置

@ApplicationPath("/*")
public class ApplicationConfig extends Application {

}

Tomcat 在端口 8080 上运行,这是我发送的 Postman 请求: localhost:8080/rest/sayhi
回应

"status": 404,
    "error": "Not Found",
    "message": "No message available",

这是POM.XML

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

修复 任何人都坚持这个问题这里是修复 - >正如@cassiomolin建议的那样 在您的 ApplicationConfig 中,将扩展的 class 从 Application 更改为 -> ResourceConfig,然后添加:

@Component
public class JerseyConfig() extends ResourceConfig{
    // I also changed the name of class to JerseyConfig (this is optional of course )

     public JerseyConfig() {
         register(JustService.class);
     }
     //change JustService to your service class and add as many register's as service classes you have.
    // you can add @ApplicationPath annotation to this class if you need to

删除 spring-boot-starter-web 依赖项并仅使用 spring-boot-starter-jersey 依赖项。然后用 @Component:

注释 JustService class
@Component
@Path("/rest")
public class JustService {
    ...
}

最后将以下 class 添加到您的申请中:

@Component
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        register(JustService.class);
    }
}

更多详细信息,请参阅Spring引导documentation