2017-11-29 2 views
0

Ich habe einen Webservice mit Jersey in einem dynamischen Webprojekt erstellt. Jetzt möchte ich es bauen, ohne Maven zu benutzen (unser Professor wird uns Maven nicht benutzen lassen). Ich führe das Projekt auf einem Tomcat 9.0 Server. Es läuft perfekt, aber wenn ich auf localhost:8080/agendarest/contactos zugreifen, bekomme ich einen 404 Fehler.Dynamisches Webprojekt ohne Maven erstellen

Service:

@Path("/contactos") 
public class ContactoService { 
    private static final long serialVersionUID = 1L; 

    @GET 
    @Produces("application/json") 
    public Response getUsers() throws JSONException{ 
     List<Contacto> contactos = new ArrayList<>(); 
     contactos.add(new Contacto("pedro", 3434661825L)); 
     return Response.status(200).entity(contactos.toString()).build(); 
    } 
} 

web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" 
     version="3.1"> 
    <display-name>agendarest</display-name> 
    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
     <welcome-file>index.htm</welcome-file> 
     <welcome-file>index.jsp</welcome-file> 
     <welcome-file>default.html</welcome-file> 
     <welcome-file>default.htm</welcome-file> 
     <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
     <servlet-name>Jersey Web Application</servlet-name> 
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey Web Application</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 
</web-app> 
+0

Hat Ihr Professor auch Gradle nicht zulassen? – azurefrog

+0

Ja, er sagte uns, dass wir es ohne irgendein Framework ausführen könnten, aber nicht wie ... –

+0

funktioniert localhost: 8080/agendarest? läuft deine Web-App? ist die web.xml im Ordner WEB-INF? – Yussef

Antwort

0

ich die Lösung gefunden. Ich brauchte init-param Also, meine web.xml hinzuzufügen:

<servlet> 
       <servlet-name>ContactoService</servlet-name> 
       <init-param> 
        <param-name>javax.ws.rs.Application</param-name> 
        <param-value>com.MiApp</param-value> 
       </init-param> 
       <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
       <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
       <servlet-name>ContactoService</servlet-name> 
       <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

Und hinzufügen MiApp.java:

import org.glassfish.jersey.server.ResourceConfig; 

public class MiApp extends ResourceConfig{ 
    public MiApp() { 
     packages("com"); 
    } 
} 
Verwandte Themen