2012-05-08 17 views
11

Questa è la mia prima volta che utilizzo Eclipse, e mi sta facendo impazzire molto.Rest Web service che restituisce un 404

Ho installato Tomcat 6.0, scaricato le librerie Jersey, e ho seguito il tutorial da: http://www.vogella.com/articles/REST/article.html#first_client

ho creato il nome del progetto come RestExample, e all'interno di tale ho un pacchetto di de.jay.jersey.first ed entro che ho un HelloWorldResource di classe, ed ecco come appare:

package de.jay.jersey.first; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

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

// This method is called if XML is request 
@GET 
@Produces(MediaType.TEXT_XML) 
public String sayXMLHello() { 
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>"; 
} 

// This method is called if HTML is request 
@GET 
@Produces(MediaType.TEXT_HTML) 
public String sayHtmlHello() { 
    return "<html> " + "<title>" + "Hello Jersey" + "</title>" 
      + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> "; 
} 
} 

e il mio web.xml sembra che

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>RestExample</display-name> 
    <servlet> 
<servlet-name>Jersey REST Service</servlet-name> 
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
<init-param> 
    <param-name>com.sun.jersey.config.property.packages</param-name> 
    <param-value>de.jay.jersey.first</param-value> 
</init-param> 
<load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
<servlet-name>Jersey REST Service</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
</servlet-mapping> 
</web-app> 

e sto cercando di usare curl come:

ricciolo http://localhost:8081/RestExample/rest/hello

Apache Tomcat/6.0.35 - Rapporto d'errore

HTTP Stato 404 -/RestExample/resto/Ciao

tipo stato ri porta

messaggio/RestExample/resto/ciao

de scrizione La risorsa richiesta (/ RestExample/riposo/ciao) è non è disponibile.

Apache Tomcat/6.0.35

La domanda è: cosa devo modificare nel file web.xml in modo che possa accedere a tale risorsa?

Ho provato RestExample/de.jay.jersey.first/rest/hello, e ancora non ha funzionato. TOmcat è in esecuzione senza errori.

+0

Come è stata distribuita questa applicazione su Tomcat? In particolare, dove si trova il tuo sito web.xml e dove si trova il tuo file di classe? –

+0

Ho provato Tomcat 7.0 e funziona perfettamente – DaTroop

+0

Ho modificato solo elementi minimi come la versione Servlet da 2.5 a 3.0 in web.xml – DaTroop

risposta

1

ho provato con Tomcat 7.0 e funziona benissimo:

package de.jay.jersey.first; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

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

// This method is called if XML is request 
    @GET 
    @Produces(MediaType.TEXT_XML) 
    public String sayXMLHello() { 
     return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>"; 
    } 

// This method is called if HTML is request 
    @GET 
    @Produces(MediaType.TEXT_HTML) 
    public String sayHtmlHello() { 
     return "<html> " + "<title>" + "Hello Jersey" + "</title>" 
       + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> "; 
    } 
} 

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <display-name>RestExample</display-name> 
    <servlet> 
     <servlet-name>Jersey REST Service</servlet-name> 
     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>de.jay.jersey.first</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey REST Service</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

Browsed per http://localhost:8084/RestExample/rest/hello e funziona bene

+0

Questa configurazione è buona se sei su Jersey 1.x. Jersey 2.x ha nomi di pacchetti e nomi di pacchetti diversi. Quindi, la stessa configurazione non funzionerà se si utilizzano i vasi 2.x. Ho solo pensato di menzionarlo, in modo da risparmiare inutili problemi a qualcuno che ha 2.x vasetti nel suo classpath. – adityalad

-2

come l'aticle dice:

// Get the Todos 
    System.out.println(service.path("rest").path("todos").accept(
      MediaType.TEXT_XML).get(String.class)); 
    // Get XML for application 
    System.out.println(service.path("rest").path("todos").accept(
      MediaType.APPLICATION_JSON).get(String.class)); 
    // Get JSON for application 
    System.out.println(service.path("rest").path("todos").accept(
      MediaType.APPLICATION_XML).get(String.class)); 

si tenta di specificare il percorso del metodo che si desidera chiamare

12

Mi ci è voluto un sacco di tempo per capire perché non funzionava per me, nonostante la ricerca in tutto il web per la soluzione. L'errore che stavo facendo era che i nomi dei pacchetti non erano aggiornati per la nuova maglia api. Ecco cosa nomi dei pacchetti aggiornati dovrebbe essere simile (Web.xml):

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 
    <display-name>RestExample</display-name> 
    <servlet> 
     <servlet-name>Jersey REST 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>de.jay.jersey.first</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Jersey REST Service</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

noti che <servlet-class> e <param-name> sono diversi (aggiornato) da vogella tutorial. Potrebbe non essere la risposta a questa particolare domanda ma potrebbe aiutare qualcuno. L'ho trovato da here.

+0

grazie @gndps hai salvato il mio giorno. La tua risposta merita di essere al primo posto. Ho cambiato il progetto e spostato anche la parte web.xml, ma ho dimenticato di cambiare il nome del pacchetto. La tua risposta mi ha aiutato. Grazie ancora – samir

+0

@samir - cosa significa gndps non è il nome del pacchetto, ma il nome param di init in cui viene fornito il nome del pacchetto in web.xml. comunque la correzione che hai fatto è valida per il tuo problema, ma leggermente diversa dal problema spiegato qui. Il nome init-param per menzionare i pacchetti si differenzia con diverse versioni di jersey. –

2

Si prega di aggiungere tutti i barattoli indicate nel progetto

progetto (Right Click)> Proprietà> Java Build Path> Biblioteche> Aggiungi JAR/Aggiungi JAR esterni

  1. asm-3.1.jar
  2. jersey-fascio-1.14.jar
  3. jersey-client.jar
  4. jersey-core.1.17.1.jar
  5. jersey-server-1.17.jar
  6. jersey-servlet-1.17.jar
0

ho cercato una soluzione per lo stesso problema per ore anche.

questo risolto il problema:

se si utilizza un Maven-Project (per esempio con archetipo Maven-archetipo-webapp) e il HelloWorldResource classe è implementata nella cartella src/main/risorse questa classe doesn 't ottenere compilato (per esempio, allora l'esecuzione di "mvn pacchetto pulito" o "eseguito su server" in Eclipse)

Implementare HelloWorldResource nella cartella src/main/java invece e non più 404 occures .. (se si crea Maven-Project con maven-archetype-webapp la cartella deve essere creata manualmente)

1

Verificare se il percorso ha questo bar '/' esempio: @Path ('/ path') in alcuni casi questo problema è solo il bar manca!

Problemi correlati