繁体   English   中英

Java REST-404资源找不到

[英]Java REST - 404 resource can not be found

我正在尝试访问Hello World(目前)应用程序上的资源。 但是,我得到的HTTP状态为404-在Tomcat上部署它并单击URL:localhost:8080 / test / rest时找不到。 这是我的pom.xml依赖关系:

<dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.26-b03</version>
</dependency>
<dependency>
       <groupId>org.glassfish.jersey.core</groupId>
       <artifactId>jersey-server</artifactId>
       <version>2.26-b03</version>
</dependency>
<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
</dependency>
<dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.26-b03</version>
</dependency>
<dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.11</version>
       <scope>test</scope>
</dependency>

这是我的web.xml配置:

<web-app>
    <display-name>Archetype Created Web Application</display-name>
<servlet>
    <servlet-name>HelloWorld Jersey Service </servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
    <param-name >jersey.config.server.provider.packages</param-name>
    <param-value >com.latin.translator.app</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name >HelloWorld Jersey Service </servlet-name >
    <url-pattern >/test/*</url-pattern >
</servlet-mapping>
</web-app>

Java代码是:

@Path("/test")
public class Test {

    @GET
    @Produces(TEXT_PLAIN)
    @Path("/rest")
    public String test() {
        return "Great success";
    }
}

您有什么想法我做错了吗?

TRy本地主机:8080 / test / test / rest

<servlet-mapping>
    <servlet-name >HelloWorld Jersey Service </servlet-name >
    <url-pattern >/test/*</url-pattern >
</servlet-mapping>

这表示root是test,因此jesrsey的url是localhost:8080 / test /

现在

@Path("/test")
public class Test {

说现在下一个URL路径是测试,所以URL是本地主机:8080 / test / test

@Path("/rest")
    public String test() {
        return "Great success";
    }

说/ rest作为下一个网址,所以它的本地主机:8080 / test / test / rest

您需要扩展javax.ws.rs.core.Application类。

@ApplicationPath("/") // the context root of you application
public class JaxRsConfig extends Application {

      private final Set<Class<?>> classes;

      public JaxRsConfig() {
        HashSet<Class<?>> c = new HashSet<>();
        c.add(Test.class); //repeat for all JAX-RS classes in your application

        classes = Collections.unmodifiableSet(c);
      }

      @Override
      public Set<Class<?>> getClasses() {
        return classes;
      }

}

这样,您就不需要web.xml文件。

由于指定了提供程序包,因此您可以尝试使用以下url-localhost:8080 / com.latin.translator.app / test / rest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM