我正在构建一个 JAX-RS 项目,它使用配置文件重定向到我定义了 HTTP 方法的邮件类。在调试时,控制转到配置文件,如下所示:
package com.ODSResrcSysApp;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class ODSResrcSysConfig extends Application
{
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(ODSResrcSysSvcREST.class);
return classes;
}
}
从这里开始,控件应该转到 ODSResrcSysSvcREST.class,其中 uri 模式定义如下。
package com.ODSResrcSysApp;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Clob;
import java.sql.ResultSet;
import java.util.Properties;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/*")
public class ODSResrcSysSvcREST
{
@GET
@Path("/Test")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public String gettstCon() throws WebApplicationException
{
System.out.println("Entering gettstCon method.");
String result = null;
ODSConnection odsconn = new ODSConnection();
try
{
result = odsconn.connect();
if(result == "connection successful")
{
result = "<ResourceSystem>Connection established successfully</ResourceSystem>";
}
else
{
result = "<Error>Error occured while establishing connection. If issues persists, contact CTS SOA team.</Error>";
}
}
catch (Exception ex)
{
result = ex.getMessage().toString();
}
return result;
}
}
此时出现以下错误:
com.sun.jersey.server.impl.application.WebApplicationImpl _handleRequest
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
Throwable occurred: java.lang.StringIndexOutOfBoundsException
at java.lang.String.substring(String.java:1093)
at com.sun.jersey.server.impl.application.WebApplicationContext.pushRightHandPathLength(WebApplicationContext.java:282)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:130)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:736)
Sep 21, 2012 2:02:28 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet ODS Resource System JAX-RS REST Service threw exception
Throwable occurred: java.lang.StringIndexOutOfBoundsException
at java.lang.String.substring(String.java:1093)
at com.sun.jersey.server.impl.application.WebApplicationContext.pushRightHandPathLength(WebApplicationContext.java:282)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:130)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:736)
这个项目的 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>ODS_ResrcSysApp</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>ODS Resource System JAX-RS REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.ODSResrcSysApp.ODSResrcSysConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ODS Resource System JAX-RS REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
不确定是什么导致了错误。同一工作空间中的另一个 JAX-RS 项目可以正常工作,即使它使用相同的 jar 文件集。通过代码进行调试时,我还看到了以下页面:
DeferredResourceConfig.class
The source attachment does not contain the source fir the file DeferredResourceConfig.class .......
我的项目中包含的 jar 文件列表是:
1. asm-3.1.jar
2. cloveretl.engine.jar
3. commons-lang.jar
4. ibm-wink-jaxrs.jar
5. jackson-core-asl-1.8.3.jar
6. jackson-jaxrs-1.8.3.jar
7. jackson-mapper-asl-1.8.3.jar
8. jackson-xc-1.8.3.jar
9. jersey-client-1.9.1.jar
10. jersey-core-1.9.1.jar
11. jersey-json-1.9.1.jar
12. jersey-server-1.9.1.jar
13. jettison-1.1.jar
14. jsr311-api.jar
15. jsr311-api-1.1.1.jar
16. madapi.jar
17. slf4j-api.jar
18. slf4j-jdk14.jar
非常感谢您的回答。先感谢您。