带有Tomcat的简单JAX-RS-找不到404(没有web.xml)

问题描述 投票:0回答:2

SERVICE

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

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

应用程序配置

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

}

Tomcat在端口8080上运行,这是我正在发送的邮递员请求: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>
java tomcat jersey jax-rs
2个回答
1
投票

删除spring-boot-starter-web依赖性,仅使用spring-boot-starter-jersey依赖性。然后用JustService注释@Component类:

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

最后将以下类添加到您的应用程序:

@Component
public class JerseyConfig extends ResourceConfig {

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

有关更多详细信息,请参阅Spring Boot documentation


0
投票

您大声告诉其余的api查找服务的资源。

尝试将此方法添加到您的ApplicationConfig中:

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> set = new HashSet<>();
    set.add(JustService.class);
    return set;
}

如果不起作用,请尝试将您的ApplicationConfig映射到特定路径,例如:

@@ ApplicationPath(“ / jaxrs”)

当然,您的测试网址将会是

localhost:8080 / jaxrs / rest / sayhi

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.