使用 RESTful Web 服务和 Jersey 创建 Hello World
我按照教程此处了解如何创建Web服务使用 RESTful Web 服务和 Jersey,我有点卡住了。该代码来自我上面链接的教程中的 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 进行调试,并发现 HelloGreetingService.java
中的这一行 return new RESTGreeting(getGreeting(), name);
导致了异常。但不知道为什么。这是堆栈跟踪
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)
I follow tutorial here on how to create web service using RESTful web service and Jersey and I get kind of stuck. The code is from HelloWorld3 in the tutorial I linked above. Here is the code. I use Netbean6.8 + glassfish v3
RESTGreeting.java
create using JAXB
. This class represents the HTML message in 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
creates a RESTful web service that returns an HTML message
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) {
}
}
However when i deploy it on Glassfish, and run it. It generate an exception. I try to debug using netbean 6.8, and figure out that this line return new RESTGreeting(getGreeting(), name);
in HelloGreetingService.java
cause the exception. But not sure why. Here is the 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的资源方法返回 JAXB 注释类的实例,并且将无法生成 HTML(该示例在普通 java 字符串中返回 HTML)。这不是您想要的 XML 吗?在这种情况下,将 getHtml 上的注释更改为
@Produces({"application/xml"})
Your resource method returns an instance of a JAXB-annotated class and will not be able to produce HTML (the example returns HTML in an ordinary java String). Isn't it XML you want? In that case change the annotation on getHtml to
@Produces({"application/xml"})
在 Eclipse 中
http://www.brucephillips.name/blog/index.cfm/2009/5/28/An-Introduction-to-Creating-RESTful-Web-Services-Using-Jersey-and -Java#comments
如果您不使用 maven,则将提到的 jar 放在 WEB-INF/lib 文件夹中(如果未预设 lib 文件夹),则创建一个放置 jar 的位置。
还要确保 jar 版本与文档中提到的相同,否则会出错。
如果您不想创建运行良好的应用程序,请下载 zip 文件。
如果您想要 JAR 文件,请联系我。
In eclipse
http://www.brucephillips.name/blog/index.cfm/2009/5/28/An-Introduction-to-Creating-RESTful-Web-Services-Using-Jersey-and-Java#comments
If you are not using maven then place the jars mentioned in WEB-INF/lib folder if lib folder is not preset create one an place the jars.
Also make sure the jar version is same as mentioned in the document or else it gives error.
Download the zip file if you dont want to create the application that works great.
If you want the JAR files contact me.