2017-09-18 1 views
-1

J'essaie de créer un simple projet Rest API avec Tomcat, JAX-RS et Jersey. Le problème auquel je suis confronté est que j'obtiens l'erreur 404 du navigateur Web, quand j'effectue un get. Ce sont mes fichiers:Erreur 404 sur l'exemple Tomcat Rest

web.xml

<?xml version = "1.0" encoding = "UTF-8"?> 
<web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
    xmlns = "http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id = "WebApp_ID" version = "3.0"> 
    <display-name>User Management</display-name> 
    <servlet> 
     <servlet-name>Jersey RESTful Application</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.rest</param-value> 
     </init-param> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey RESTful Application</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

src/main/java j'ai à paquets: com.example où j'ai personne (le modèle de l'application) et RestApplication, alors que dans com.rest J'ai PersonEndpoint.java:

@Path("/persons") 
@Produces("application/json") 
@Consumes("application/json") 
public class PersonEndpoint { 

    @POST 
    public Response create(final Person person) { 
     // TODO: process the given person 
     // you may want to use the following return statement, assuming that 
     // Person#getId() or a similar method 
     // would provide the identifier to retrieve the created Person resource: 
     // return 
     // Response.created(UriBuilder.fromResource(PersonEndpoint.class).path(String.valueOf(person.getId())).build()).build(); 
     return Response.created(null).build(); 
    } 

    @GET 
    @Path("/{id:[0-9][0-9]*}") 
    public Response findById(@PathParam("id") final Long id) { 
     // TODO: retrieve the person 
     Person person = null; 
     if (person == null) { 
      return Response.status(Status.NOT_FOUND).build(); 
     } 
     return Response.ok(person).build(); 
    } 

    @GET 
    public List<Person> listAll() { 
     // TODO: retrieve the persons 
     final List<Person> persons = new ArrayList<>(); 
     persons.add(new Person("Mario", "Rossi", 123)); 
     return persons; 
    } 

    @PUT 
    @Path("/{id:[0-9][0-9]*}") 
    public Response update(@PathParam("id") Long id, final Person person) { 
     // TODO: process the given person 
     return Response.noContent().build(); 
    } 

    @DELETE 
    @Path("/{id:[0-9][0-9]*}") 
    public Response deleteById(@PathParam("id") final Long id) { 
     // TODO: process the person matching by the given id 
     return Response.noContent().build(); 
    } 

} 

RestApplication.java

@ApplicationPath("/rest") 
public class RestApplication extends Application { 

} 

L'URL que je suis en train de u se est: http://localhost:8080/People/rest/persons

Où est-ce que je me trompe?

Répondre

1

Vous devez vous inscrire End Point lié à votre chemin, changement RestApplication:

public class RestApplication extends ResourceConfig { 
public RestApplication() { 
    register(PersonEndpoint.class); 
} 

Person ne fait pas partie du chemin, vous devez appeler http://localhost:8080/rest/persons

+0

Merci pour votre réponse, mais malheureusement, il ne résout pas les erreurs 404 –

+0

Vous devriez appeler http: // localhost: 8080/rest/persons – user7294900

+0

Cela ne fonctionne pas ... –