2010-05-23 8 views
0

Je suis le tutoriel here sur la façon de créer un service web en utilisant le service web RESTful et Jersey et je suis coincé. Le code provient de HelloWorld3 dans le tutoriel que j'ai lié ci-dessus. Voici le code. J'utilise Netbean6.8 + glassfish v3Créer Hello World avec le service web RESTful et Jersey

RESTGreeting.java créer en utilisant JAXB. Cette classe représente le message HTML en Java

package com.sun.rest; 

import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlElement; 


@XmlRootElement(name = "restgreeting") 
public class RESTGreeting { 
private String message; 
private String name; 

/** 
* Creates new instance of Greeting 
*/ 
public RESTGreeting() { 
} 

/* Create new instance of Greeting 
* with parameters message and name 
*/ 
public RESTGreeting(
    String message, 
    String name) { 
    this.message = message; 
    this.name = name; 
} 

/** Getter for message 
* return value for message 
* 
*/ 
@XmlElement 
public String getMessage() { 
    return message; 
} 

public void setMessage(String message) { 
    this.message = message; 
} 

/* Getter for name 
* return name 
*/ 
@XmlElement 
public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 
} 

HelloGreetingService.java crée un service Web RESTful qui renvoie un message HTML

package com.sun.rest; 

import javax.ws.rs.core.Context; 
import javax.ws.rs.core.UriInfo; 
import javax.ws.rs.Consumes; 
import javax.ws.rs.PUT; 
import javax.ws.rs.Path; 
import javax.ws.rs.GET; 
import javax.ws.rs.Produces; 
import javax.ws.rs.QueryParam; 


@Path("helloGreeting") 
public class HelloGreetingService { 
@Context 
private UriInfo context; 

/** Creates a new instance of HelloGreetingService */ 
public HelloGreetingService() { 
} 

/** 
* Retrieves representation of an instance of com.sun.rest.HelloGreetingService 
* @return an instance of java.lang.String 
*/ 
@GET 
@Produces("text/html") 
public RESTGreeting getHtml(@QueryParam("name") 
String name) { 
    return new RESTGreeting(
     getGreeting(), 
     name); 
} 

private String getGreeting() { 
    return "Hello "; 
} 

/** 
* PUT method for updating or creating an instance of HelloGreetingService 
* @param content representation for the resource 
* @return an HTTP response with content of the updated or created resource. 
*/ 
@PUT 
@Consumes("text/html") 
public void putHtml(String content) { 
} 
} 

Cependant quand je le déployer sur Glassfish et l'exécuter. Il génère une exception. J'essaie de déboguer en utilisant netbean 6.8, et de comprendre que cette ligne return new RESTGreeting(getGreeting(), name); en HelloGreetingService.java cause l'exception. Mais je ne sais pas pourquoi. Voici le stacktrace

javax.ws.rs.WebApplicationException 
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:268) 
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1029) 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941) 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932) 
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384) 
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451) 
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188) 
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641) 
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97) 
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185) 
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165) 
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791) 
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693) 
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954) 
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170) 
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88) 
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76) 
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53) 
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57) 
at com.sun.grizzly.ContextTask.run(ContextTask.java:69) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309) 
at java.lang.Thread.run(Thread.java:637) 

Répondre

3

Votre méthode de ressource renvoie une instance d'une classe JAXB annoté et ne sera pas en mesure de produire du HTML (l'exemple retourne HTML dans une chaîne java ordinaire). N'est-ce pas XML que tu veux? Dans ce cas, changer l'annotation sur getHTML à

@Produces({"application/xml"})

+0

tyvm, qui corrige, mais si je veux html afin que je puisse afficher sur le navigateur web, puis-je convertir JAXB en chaîne en quelque sorte? –

+0

Non, pas que je sache. Toutefois, Jersey prend en charge le rendu de page à l'aide de JSP (http://blogs.sun.com/sandoz/entry/mvcj) mais n'utilise pas vos annotations JAXB pour cela. S'agit-il d'une application Web classique que vous créez ou d'un service Web? –

1

éclipse

http://www.brucephillips.name/blog/index.cfm/2009/5/28/An-Introduction-to-Creating-RESTful-Web-Services-Using-Jersey-and-Java#comments

Si vous ne l'utilisez Maven puis placez les pots mentionnés dans le dossier WEB-INF/lib si le dossier lib n'est pas préréglé créer un endroit les pots.

Assurez-vous également que la version du pot est la même que celle mentionnée dans le document, sinon elle donne une erreur.

Téléchargez le fichier zip si vous ne voulez pas créer l'application qui fonctionne très bien. Si vous voulez que les fichiers JAR me contactent.