简体   繁体   中英

Java JAX-RS 404 not found

For some reason I get 404 every time I try to call the service at http://localhost:8080/{context}/v1/{name} . I'm using TomEE 8.5 and JEE 7. And yes, I've read the other questions on stackoverflow, In case you want to declare it as duplicate of x. Since this is my first time trying jax-rs/jersey, I wasn't completely sure which dependencies to add, so I've the ones that were suggested by people here.

<servlet>
    <servlet-name>ApplicationRestService</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.vio.project.controller</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
   <servlet-mapping>
      <servlet-name>ApplicationRestService</servlet-name>
     <url-pattern>/v1/*</url-pattern>

<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-server -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.26</version>
</dependency> 
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-client -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.26</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.core/jersey-common -->
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-common</artifactId>
    <version>2.26</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.19.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet -->
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.26</version>
</dependency> 
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.containers/jersey-container-servlet-core -->
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.26</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>2.26</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.bundles.repackaged</groupId>
  <artifactId>jersey-guava</artifactId>
  <version>2.6</version>

-

@Path("/jobs")
public class JobsApi {

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public JsonObject testMethod() {
      return Json.createObjectBuilder().add("hello", "world").build();
  }
}

You should also create some class to serve the request like:

@Path("/hello")
public class Hello {

  // This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }
}

And try the URL like: http://localhost:8080/ {context}/v1/hello

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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