gpt4 book ai didi

java - 实现基本的 REST API - ServletException

转载 作者:行者123 更新时间:2023-11-28 22:59:12 27 4
gpt4 key购买 nike

我正在尝试在 Eclipse 上运行示例基本 REST API,但一直收到 ServletException

代码如下:

员工.java

package net.javabeat.articles.spring.rest.simple;

public class Employee {

private String id;
private String name;
private String department;

public Employee(){}

public Employee(String id, String name, String department){
this.id = id;
this.name = name;
this.department = department;
}

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}

public String toString(){

StringBuilder result = new StringBuilder();

result.append("[employee]");
result.append("[id]" + id + "[id]");
result.append("[name]" + name + "[name]");
result.append("[department]" + department + "[department]");
result.append("[employee]");
return result.toString();
}

public String toXmlString(){

StringBuilder result = new StringBuilder();

result.append("<employee>");
result.append("<id>" + id + "</id>");
result.append("<name>" + name + "</name>");
result.append("<department>" + department + "</department>");
result.append("</employee>");
return result.toString();
}

}

员工经理.java

package net.javabeat.articles.spring.rest.simple;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("employee-manager")
public class EmployeeManager {

private static Set<Employee> employees;

@GET
@Produces(MediaType.TEXT_XML)
@Path("employeelist")
public String getEmployeesInXmlFormat(){

StringBuilder xmlResult = new StringBuilder();
xmlResult.append("<employees>");

Iterator<Employee> iterator = employees.iterator();
while (iterator.hasNext()){

Employee employee = iterator.next();
String employeeAsXml = employee.toXmlString();
xmlResult.append(employeeAsXml);
}
xmlResult.append("</employees>");
return xmlResult.toString();
}

@GET
@Produces(MediaType.TEXT_PLAIN)
@Path("employeelist")
public String getEmployeesInPlainFormat(){

StringBuilder plainResult = new StringBuilder();
plainResult.append("[employees]");

Iterator<Employee> iterator = employees.iterator();
while (iterator.hasNext()){

Employee employee = iterator.next();
String employeeAsXml = employee.toXmlString();
plainResult.append(employeeAsXml);
}
plainResult.append("[employees]");
return plainResult.toString();
}

static{
employees = new HashSet<Employee>();

employees.add(new Employee("1", "David", "IT"));
employees.add(new Employee("2", "John", "SALES"));
}
}

EMClient.java

package net.javabeat.articles.spring.rest.simple;

import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class EMClient {

public static void main(String[] args) throws Exception{

ClientConfig clientConfig = new DefaultClientConfig();
Client clientObject = Client.create(clientConfig);

String uriPath = "http://localhost:8080/REST-Simple/rest-simple";
URI uriObject = UriBuilder.fromUri(uriPath).build();
WebResource employeeResource = clientObject.resource(uriObject);

xmlFormatTest(employeeResource);
plainFormatTest(employeeResource);
}

private static void xmlFormatTest(WebResource employeeResource){

WebResource innerResource = employeeResource.path("employee-manager").path("employeelist");
WebResource.Builder builderObject = innerResource.accept(MediaType.TEXT_XML);
String employeesAsXml = builderObject.get(String.class);
System.out.println(employeesAsXml);
}

private static void plainFormatTest(WebResource employeeResource){

WebResource innerResource = employeeResource.path("employee-manager").path("employeelist");
WebResource.Builder builderObject = innerResource.accept(MediaType.TEXT_PLAIN);
String employeesAsPlainText = builderObject.get(String.class);
System.out.println(employeesAsPlainText);
}
}

部署描述符web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>REST-EmployeeManager</display-name>
<servlet>
<servlet-name>REST-Simple</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>net.javabeat.articles.spring.rest.simple</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>REST-Simple</servlet-name>
<url-pattern>/rest-simple/*</url-pattern>
</servlet-mapping>
</web-app>

在尝试访问 http://localhost:8080/Rest-EmployeeManager/rest-simple/employee-manager/employeelist 时显示以下内容:

HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet REST-Simple threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)


root cause

java.lang.NoClassDefFoundError: Could not initialize class sun.proxy.$Proxy8
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
java.lang.reflect.Constructor.newInstance(Unknown Source)
java.lang.reflect.Proxy.newInstance(Unknown Source)
java.lang.reflect.Proxy.newProxyInstance(Unknown Source)
com.sun.jersey.server.impl.application.WebApplicationImpl$26.run(WebApplicationImpl.java:1626)
java.security.AccessController.doPrivileged(Native Method)
com.sun.jersey.server.impl.application.WebApplicationImpl.createProxy(WebApplicationImpl.java:1623)
com.sun.jersey.server.impl.application.WebApplicationImpl.<init>(WebApplicationImpl.java:335)
com.sun.jersey.server.impl.container.WebApplicationProviderImpl.createWebApplication(WebApplicationProviderImpl.java:55)
com.sun.jersey.spi.container.WebApplicationFactory.createWebApplication(WebApplicationFactory.java:66)
com.sun.jersey.spi.container.servlet.ServletContainer.create(ServletContainer.java:394)
com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.create(ServletContainer.java:309)
com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:603)
com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:376)
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:559)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.27 logs.


--------------------------------------------------------------------------------

Apache Tomcat/7.0.27

对我可能做错的事情有什么帮助吗?

最佳答案

我看到你关注了 tutorial .我在其中找不到 Jersey 版本,但根据文章的日期,我认为他们没有使用 Jersey 2.x。如果您确实使用最新的,请使用 Jersey 1.x 尝试您的项目.我让它与 GlassFish 3.1.2.2 捆绑在一起的 1.11.1 一起工作。

关于java - 实现基本的 REST API - ServletException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22034910/

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