java.lang.ClassNotFoundException:com.sun.jersey.core.spi.factory.ResponseImpl REST-FUL Webservices

时间:2013-08-27 23:53:07

标签: java rest maven jersey

我正在尝试使用 Jersey 运行 REST-FUL Java GET方法,没有任何问题,但是当我将任何方法从GET更改为POST时,我Web服务器抛出错误

java.lang.ClassNotFoundException: com.sun.jersey.core.spi.factory.ResponseImpl

这是我的代码类实现:

@Path("/entrega")
public class EntregaWebServiceREST extends WebServiceParentREST {

    static Logger log = Logger.getLogger(EntregaWebServiceREST.class.getName());
    private EntregaService entregaService;



    public EntregaWebServiceREST() {

        entregaService = context.getBean(EntregaService.class);
    }



    @POST 
    @Produces("text/plain")
    @Consumes("text/xml")
    @Path("/sendWorkerToClient")
    public String setWorkerToClient(@RequestParam("xml")
                                            String xml) {

        try {
            return entregaService.setWorkerToClient(xml);
        } catch (Exception e) {
            log.error(e);
            e.printStackTrace();
            return "ocurrio una excepcion: " + e.getMessage();
        }
    }


}

这是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hanhelds</groupId>
    <artifactId>OfficeDepot-REST</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <zk.version>6.5.2</zk.version>
        <spring.version>3.1.2.RELEASE</spring.version>
        <hibernate.version>4.0.0.Final</hibernate.version>
        <commons-io>1.3.1</commons-io>
        <maven.build.timestamp.format>yyyy-MM-dd</maven.build.timestamp.format>
        <packname>-${project.version}-FL-${maven.build.timestamp}</packname>

        <jersey-version>1.13</jersey-version>        
        <xstream.version>1.4.4</xstream.version>
        <!-- del proyecto jdbc -->
        <version.log4j>1.2.17</version.log4j>
        <version.slf4j>1.7.2</version.slf4j>
        <version.sql-processor>2.2.2</version.sql-processor>
        <!-- del proyecto jdbc -->    
        <m2eclipse.wtp.contextRoot>/services</m2eclipse.wtp.contextRoot>


    </properties>
    <packaging>war</packaging>
    <name>The Handhelds Project</name>
    <description>The Handhelds Project</description>
    <licenses>
        <license>
            <name>GNU LESSER GENERAL PUBLIC LICENSE, Version 3</name>
            <url>http://www.gnu.org/licenses/lgpl.html</url>
            <distribution>repo</distribution>
        </license>
    </licenses>
    <repositories>      
        <!-- para Jersey -->
        <repository>
            <id>glassfish.java.net</id>
            <name>GlassFish Maven Repository</name>
            <url>http://download.java.net/maven/glassfish</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>m2.java.net</id>
            <name>Java.net Maven 2 Repository</name>
            <url>http://download.java.net/maven/2</url>
            <layout>default</layout>
        </repository>
        <!-- para Jersey -->

    </repositories>
    <pluginRepositories>

        <!-- para Jersey -->    
        <pluginRepository>
            <id>m2.java.net</id>
            <name>Java.net Maven 2 Repository</name>
            <url>http://download.java.net/maven/2</url>
            <layout>default</layout>
        </pluginRepository>
        <!-- para Jersey -->        
    </pluginRepositories>
    <dependencies>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>${commons-io}</version>
        </dependency>

        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- JPA(Hibernate) and HSQL dependencies -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.2.6</version>
        </dependency>


    <!-- dependencias para Jersey -->

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey-version}</version>
        </dependency> -->

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-json</artifactId>
            <version>${jersey-version}</version>
        </dependency> -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey-version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.glassfish.distributions</groupId>
            <artifactId>web-all</artifactId>
            <version>10.0-build-20080430</version>
            <scope>test</scope>
        </dependency>
         <!--
        <dependency>
            <groupId>org.glassfish.embedded</groupId>
            <artifactId>gf-embedded-api</artifactId>
            <version>1.0-alpha-4</version>
            <scope>test</scope>
        </dependency>
         -->
    <!-- dependencias para Jerysey -->


    <dependency>
        <groupId>com.thoughtworks.xstream</groupId>
        <artifactId>xstream</artifactId>
        <version>${xstream.version}</version>
    </dependency>
    <dependency>
        <groupId>xpp3</groupId>
        <artifactId>xpp3</artifactId>
        <version>1.1.4c</version>
    </dependency>
    <dependency>
        <groupId>xmlpull</groupId>
        <artifactId>xmlpull</artifactId>
        <version>1.1.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.gatein.common</groupId>
        <artifactId>common-logging</artifactId>
        <version>2.1.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>net.sf.jt400</groupId>
        <artifactId>jt400</artifactId>
        <version>6.7</version>
    </dependency>

    <!-- eliminar si agregan proyecto jdbc -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${version.slf4j}</version>
    </dependency>

    <dependency>
        <groupId>org.sqlproc</groupId>
        <artifactId>sql-processor</artifactId>
        <version>${version.sql-processor}</version>
    </dependency>


    <!-- eliminar si agregan proyecto jdbc -->
    <!-- 
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>${jersey-version}</version>
        <scope>provided</scope>
    </dependency>  -->
    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- Run withs Jetty -->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
                <configuration>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                    <stopKey>foo</stopKey>
                    <stopPort>9999</stopPort>
                    <contextPath>/services</contextPath>
                </configuration>
                <executions>
                    <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <scanIntervalSeconds>0</scanIntervalSeconds>
                            <daemon>true</daemon>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-jetty</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Compile java -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <!-- Build war -->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <groupId>org.apache.maven.plugins</groupId>
                <version>2.1.1</version>
            </plugin>
            <!-- Pack zips -->
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>webapp</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>services</finalName>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>src/main/assembly/webapp.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


            <!-- plugins oara Jersey -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <inherited>true</inherited>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.glassfish</groupId>
                <artifactId>maven-glassfish-plugin</artifactId>
            </plugin>
            <!-- plugins oara Jersey -->            
        </plugins>

    </build>    

</project>

这是堆栈跟踪错误:

[27/08/13 18:25:37:273 CDT] 0000002a servlet       E com.ibm.ws.webcontainer.servlet.ServletWrapper service Uncaught service() exception thrown by servlet Jersey Web Application: java.lang.NoClassDefFoundError: com.sun.jersey.core.spi.factory.ResponseImpl
    at java.lang.J9VMInternals.verifyImpl(Native Method)
    at java.lang.J9VMInternals.verify(J9VMInternals.java:77)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:139)
    at com.sun.jersey.core.spi.factory.AbstractRuntimeDelegate.createResponseBuilder(AbstractRuntimeDelegate.java:99)
    at javax.ws.rs.core.Response$ResponseBuilder.newInstance(Response.java:60)
    at javax.ws.rs.core.Response.status(Response.java:263)
    at com.sun.jersey.api.Responses.status(Responses.java:127)
    at com.sun.jersey.api.Responses.methodNotAllowed(Responses.java:107)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:221)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1480)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1411)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1147)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:722)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:449)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1020)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3639)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:950)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1659)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1648)
Caused by: java.lang.ClassNotFoundException: com.sun.jersey.core.spi.factory.ResponseImpl
    at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
    at com.ibm.ws.bootstrap.ExtClassLoader.findClass(ExtClassLoader.java:198)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:646)
    at com.ibm.ws.bootstrap.ExtClassLoader.loadClass(ExtClassLoader.java:113)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:612)
    at com.ibm.ws.classloader.ProtectionClassLoader.loadClass(ProtectionClassLoader.java:62)
    at com.ibm.ws.classloader.ProtectionClassLoader.loadClass(ProtectionClassLoader.java:58)
    at com.ibm.ws.classloader.CompoundClassLoader.loadClass(CompoundClassLoader.java:564)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:612)
    at com.ibm.ws.classloader.CompoundClassLoader.loadClass(CompoundClassLoader.java:564)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:612)
    ... 45 more

4 个答案:

答案 0 :(得分:4)

问题似乎是类加载器选择与应用程序不兼容的类(因此J9VMInternal.verify失败)。要解决此问题,您需要指定类加载器顺序 - 首先使用本地类加载器加载类(父级最后一个)。

答案 1 :(得分:0)

如果您想要发布的结果,请在您的课程中添加以下方法:

@POST 
@Produces("text/plain")
@Consumes("text/xml")
@Path("/sendWorkerToClient")
public String setWorkerToClient_post(@RequestParam("xml")
                                        String xml) {

    try {
        return entregaService.setWorkerToClient(xml);
    } catch (Exception e) {
        log.error(e);
        e.printStackTrace();
        return "ocurrio una excepcion: " + e.getMessage();
    }
}

答案 2 :(得分:0)

ClassNotFoundException意味着类加载器无法找到com.sun.jersey.core.spi.factory.ResponseImpl。在你的pom文件中,我没有看到对jersey core的依赖,所以你需要在你的maven pom中添加它。

答案 3 :(得分:0)

我遇到同样的问题,例如“错误500:java.lang.NoClassDefFoundError:com.sun.jersey.core.spi.factory.ResponseImpl”

但是当我将以下代码添加到web.xml文件时,它解决了这个问题。

<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<display-name>SAMPLE_WS</display-name>
	<servlet>
		<servlet-name>REST</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.sample_ws.service</param-value>
		</init-param>
		<init-param>
			<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
			<param-value>true</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>REST</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
</web-app>

@GET
@Path("msg")
@Produces(MediaType.APPLICATION_JSON)
public HelloMessage getMessage() {
	HelloMessage obj = new HelloMessage();
	obj.setMessage("Hello World from object instance");
	return obj;
}