我正在使用Apache Tomcat 9并尝试实现基本的休息服务。 我的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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Test</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
</web-app>
我的休息服务课程是
@Path("/service")
public class RestService {
@GET
@Path("/first")
@Produces(MediaType.TEXT_PLAIN)
public String generateResponse() {
return "abc";
}
@SuppressWarnings("unchecked")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/login")
public String returnJSON(String userData)
{
JSONObject returnObject=new JSONObject();
JSONParser parser = new JSONParser();
try {
JSONObject json = (JSONObject) parser.parse(userData);
final String username = (String) json.get("username");
final String password = (String) json.get("password");
returnObject.put("username", username);
returnObject.put("password", password);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnObject.toString();
}
}
但是,当我通过点击http://localhost:8080/Test/resources来调用网络服务时,会出现错误
类型例外报告
用于servlet的消息Servlet.init()[javax.ws.rs.core.Application]引发了异常
描述服务器遇到意外情况,导致无法完成请求。
异常
javax.servlet.ServletException:servlet [javax.ws.rs.core.Application]的Servlet.init()抛出异常 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:409) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:754) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1376) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:745)
如何解决这个问题?