gpt4 book ai didi

java - REST 服务 ClassNotFoundException (com.sun.jersey.spi.container.servlet.ServletContainer)

转载 作者:行者123 更新时间:2023-11-30 08:55:10 25 4
gpt4 key购买 nike

我正在使用 java 和 eclipse 来制作一个简单的休息服务。我使用 jersey 1.19 和 jackson 1.7.9。但是当我尝试使用 tomcat 运行项目时出现此异常。

SEVERE: Servlet [jersey-servlet] in web application [/EmployeeService] threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571) at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:506) at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:488) at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:115) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5262) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5550) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)

在这里,我将复制我拥有的类和 web.xml。

@XmlAccessorType(XmlAccessType.NONE)

@XmlRootElement(名称 = "雇员")公共(public)课员工{

public String empId;
public String name;
public String email;

@javax.xml.bind.annotation.XmlElement(required = true)
public String getEmpId() {
return empId;
}

public void setEmpId(String empId) {
this.empId = empId;
}

@javax.xml.bind.annotation.XmlElement(required = true)
public String getName() {
return name;
}

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

@javax.xml.bind.annotation.XmlElement(required = true)
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Path("/emp")公共(public)类 EmployeeService {

@GET
@Path("/get/{empId}")
public Employee getEmployee(@PathParam("empId") String empId) {
Employee employee = new Employee();
employee.setEmpId(empId);
employee.setName("Nikola");
employee.setEmpId("nikola@nikola.com");

return employee;
}

@POST
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_XML)
public Employee createEmployee(Employee employee) {
// create logic goes here
return employee;
}

@PUT
@Path("/update")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Employee updateEmployee(Employee employee) {
// create logic goes here
employee.setName(employee.getName() + "updated");
return employee;
}

@DELETE
@Path("/delete/{empId}")
public javax.ws.rs.core.Response deleteEmployee(@PathParam("empId") int empId) throws URISyntaxException {
return javax.ws.rs.core.Response.status(200).entity("Employee with " + empId + "is deleted successfully").build();
}

网络.xml

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"version="3.0"> 雇员供应商 索引.html 索引.htm 索引.jsp 默认.html 默认.htm 默认.jsp

<servlet>   
<servlet-name>jersey-servlet</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>com.rest.employee</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

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

最佳答案

我有同样的问题。我将所有 jar 放入引用的库中。但您必须将所有必需的 jar 文件复制粘贴到 WEB-INF/lib 文件夹中。问题解决了。

关于java - REST 服务 ClassNotFoundException (com.sun.jersey.spi.container.servlet.ServletContainer),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29185385/

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