gpt4 book ai didi

java - Jersey JAX RS 错误 - 未找到提供程序类

转载 作者:行者123 更新时间:2023-11-28 22:20:43 25 4
gpt4 key购买 nike

我是 Java Web 服务的新手,我正在尝试使用 Jersey 实现和我的 Eclipse IDE 来实现一个简单的 JAX-RS 示例,并将应用程序部署在 Apache Tomcat 6.0 上作为我的 Eclipse 的插件。我正在按照书 java web services up and running 中给出的示例进行操作

我已经创建了一个 JAX-RS 应用程序:

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资源:

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'

它说“找不到提供者类”。

当我尝试访问 URL - http://localhost:8080/rest/resourcesA/ 时,出现以下错误:

HTTP Status 404 - /rest/resourcesA/

这里的“rest”是我的eclipse项目的名称。请帮助我在这个例子中哪里做错了?

我已经浏览过这篇文章:404 when accessing Jersey app in Tomcat 7 ,但我仍然面临同样的问题。

这是一个非常基本的例子,我对为什么它不起作用感到困惑,浏览了几篇文章,但我无法找到这个问题的线索,请告诉我我哪里做错了?

最佳答案

进一步浏览后,我现在可以解决问题了。

1) 不需要 RestfulAdage 类,所以我删除了它。

2) 更新了我的 web.xml文件具有 Jersey 的 ServletContainer 的 URL 映射

<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

现在访问 URL 为 http://localhost:8080/rest/rest调用了方法 public JAXBElement<Adage> getXml()

并以 http://localhost:8080/rest/rest/plain 访问 URL调用了方法 public String getPlain()成功。

关于java - Jersey JAX RS 错误 - 未找到提供程序类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21360005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com