我是Java Web-Services的新手,我正在尝试使用我的Eclipse IDE使用Jersey实现并在Apache Tomcat 6.0上部署应用程序作为Eclipse的插件来实现一个简单的JAX-RS示例。我按照书java web services up and running
中给出的示例进行操作我创建了一个JAX-RS Application
:
package adages;
import java.util.Set;
import java.util.HashSet;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/resourcesA")
public class RestfulAdage extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> set = new HashSet<Class<?>>();
set.add(Adages.class);
return set;
}
}
和JAX-RS resource
:
package adages;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Random;
import com.fasterxml.jackson.databind.ObjectMapper;
@Path("/")
public class Adages {
private String[] aphorisms = {"What can be shown cannot be said.",
"If a lion could talk, we could not understand him."};
public Adages() {
}
@GET
@Produces({ MediaType.APPLICATION_XML })
public JAXBElement<Adage> getXml() {
return toXml(createAdage());
}
@GET
@Produces({ MediaType.APPLICATION_JSON })
@Path("/json")
public String getJson() {
return toJson(createAdage());
}
@GET
@Produces({ MediaType.TEXT_PLAIN })
@Path("/plain")
public String getPlain() {
return createAdage().toString() + "\n";
}
// Create an Adage and set the words property, which
// likewise sets the wordCount property. The adage is
// randomly selected from the array, aphorisms.
private Adage createAdage() {
Adage adage = new Adage();
adage.setWords(aphorisms[new Random().nextInt(aphorisms.length)]);
return adage;
}
// Java Adage --> XML document
@XmlElementDecl(namespace = "http://aphorism.adage", name = "adage")
private JAXBElement<Adage> toXml(Adage adage) {
return new JAXBElement<Adage>(new QName("adage"), Adage.class, adage);
}
// Java Adage --> JSON document
// Jersey provides automatic conversion to JSON using the Jackson
// libraries. In this example, the conversion is done manually
// with the Jackon libraries just to indicate how straightforward it is.
private String toJson(Adage adage) {
String json = "If you see this, there's a problem.";
try {
json = new ObjectMapper().writeValueAsString(adage);
} catch (Exception e) {
}
return json;
}
}
和我的POJO
班级:
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "adage")
public class Adage {
protected String words;
protected int wordCount;
public Adage() {
}
// overrides
@Override
public String toString() {
return words + " -- " + wordCount + " words";
}
// Setters & Getters
}
web.xml
文件包含:
<servlet>
<servlet-name>jersey</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>adages</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
当我将我的应用程序部署到Tomcat服务器时,我在日志中看到以下消息:
INFO: Starting Servlet Engine: Apache Tomcat/6.0.36
Jan 26, 2014 10:19:03 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
adages
Jan 26, 2014 10:19:03 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
class adages.Adages
Jan 26, 2014 10:19:03 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Jan 26, 2014 10:19:03 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.10 11/02/2011 03:53 PM'
它说“找不到提供者类”。
当我尝试访问网址 - http://localhost:8080/rest/resourcesA/
时,我收到以下错误:
HTTP Status 404 - /rest/resourcesA/
这里“休息”是我日食项目的名称。在这个例子中,请帮助我在哪里做错了?
我已经阅读了这篇文章:404 when accessing Jersey app in Tomcat 7,但我仍面临同样的问题。
这是一个非常基本的例子,我坚持为什么它不起作用,经历过几个帖子但是我无法弄清楚这个问题,请让我知道我在哪里做错了?
答案 0 :(得分:1)
进一步浏览后,我现在能够解决问题。
1)不需要RestfulAdage类,所以我删除了它。
2)更新了我的web.xml
文件,以获得泽西岛ServletContainer
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
现在以http://localhost:8080/rest/rest
方式访问网址会调用方法public JAXBElement<Adage> getXml()
并以http://localhost:8080/rest/rest/plain
方式访问网址,成功调用了方法public String getPlain()
。