使用RESTful Web服务和Jersey创建Hello World

时间:2010-05-23 22:42:44

标签: java-ee rest jaxb jersey netbeans6.8

我按照教程 here 了解如何使用RESTful Web服务创建Web服务,而且我遇到了麻烦。代码来自我上面链接的教程中的HelloWorld3。这是代码。我使用Netbean6.8 + glassfish v3

RESTGreeting.java使用JAXB创建。此类表示Java中的HTML消息

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创建一个返回HTML消息的RESTful Web服务

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) {
}
}

然而,当我在Glassfish上部署它并运行它时。它会产生异常。我尝试使用netbean 6.8进行调试,并发现return new RESTGreeting(getGreeting(), name);中的这一行HelloGreetingService.java会导致异常。但不确定为什么。这是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)

2 个答案:

答案 0 :(得分:3)

您的resource方法返回一个带JAXB注释的类的实例,并且无法生成HTML(该示例在普通的Java String中返回HTML)。不是你想要的XML吗?在这种情况下,将getHtml上的注释更改为

@Produces({"application/xml"})

答案 1 :(得分:1)

在日食中

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

如果您不使用maven,则将WEB-INF / lib文件夹中提到的jar放置如果未预设lib文件夹,请创建一个放置罐子的地方。

同时确保jar版本与文档中提到的相同,否则会出错。

如果您不想创建效果很好的应用程序,请下载zip文件。 如果您希望JAR文件与我联系。