一尘不染

在JAX-RS上使用UriBuilder的AbstractMethodError

java

我正在尝试使用异步响应构建REST Web服务。

我已经在网上解决了这个错误,但是,没有一种解决方案对我有用。我不确定如何去做。

这是REST服务的代码,它具有AsyncResponse,并且@Suspended取自pom.xml,我将在下面提供该文件中指定的jar文件。问题是,在部署战争时,我遇到一个例外:

java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
    javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:651)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.50 logs

我的课如下:

package com.crudapp;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import javax.annotation.Generated;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
//import javax.ws.rs.core.UriBuilder;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.gson.Gson;
import com.mysql.jdbc.StringUtils;

import dao.User;
import dao.UserDAO;
import dao.UserDAOImpl;

import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;

@Path("/crudpath")
public class EntityResource {

       private final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/spring.xml");
       UserDAO userdao = null;
       private final int numOfThreads = 10;
       private final ExecutorService executorService = Executors.newFixedThreadPool(numOfThreads);
      // userdao.getUsers("118");

       //ctx.close();
    @GET
    @Produces("application/json")
    public Response getTupleFromDBasJSON(@QueryParam("param1") String userid, @Suspended final AsyncResponse asyncresponse ){

        if(StringUtils.isNullOrEmpty(userid))
            throw new ServiceException("Userid passed to the REST service /crudpath is null or empty");
        userdao = (userdao==null)?  
                ctx.getBean("userDAO", UserDAOImpl.class)
                : userdao;
        Gson gson = new Gson();

        Future<List<User>> futures = executorService.submit(new DAOTaskHandlerThread(userid));
        List <User> users = new ArrayList<User>();
        if(futures.isDone())
        {
            try{
            users = futures.get();
            if(users!= null)
              return     Response.status(200).entity( gson.toJson(users).toString()).build();
            }
            catch(Exception ex)
            {
                throw new ServiceException(ex);
            }
        }

        return Response.status(200).entity(new ArrayList<User>().toString()).build();

        /*// crrate  a new thread.. call the DAO .. returns the result from here.
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("key", "value");
        return Response.status(200).entity( jsonObject.toString()).build();*/
    }

    private class DAOTaskHandlerThread implements Callable<List<User>>{

        //private UserDAO userDAO;
        private String userid;
        private DAOTaskHandlerThread(//UserDAO userDAO,
                String useridpassed){
            ///this.userDAO= userDAO;
            userid= useridpassed;
        }
        @Override
        public List<User> call() throws Exception {
            // TODO Auto-generated method stub
            return userdao.getUsers(userid);
        }

    }

}

pom.xml的Maven文件如下:

<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>RESTJerseyExample</groupId>
    <artifactId>RESTJerseyExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!-- spring framework just added -->

    <properties>
        <java-version>1.7</java-version>
        <org.springframework-version>4.0.3.RELEASE</org.springframework-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!-- spring framework just added ends here -->

        <dependency>
            <groupId>asm</groupId>
            <artifactId>asm</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-bundle</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-core</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.0</version>
        </dependency>
        <!--  used for httpclient library -->   
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.2</version>
        </dependency>
        <!--  for async response  -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0-m12</version>
        </dependency>
    </dependencies>
</project>

阅读 149

收藏
2020-09-08

共1个答案

一尘不染

AbstractMethodError
当应用程序试图调用一个抽象方法抛出

uri是中的抽象方法UriBuilder,因此您需要实现此方法。此方法(带有String参数)来自2.0JAX-
RS规范的版本。

您正在尝试将JAX-RS 2.0与Jersey 1. *一起使用。相反,您需要使用实现JAX-RS 2.0并包含uri方法的实现的Jersey 2. * 。

在你的pom.xml,你可能会删除这些依赖关系:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-bundle</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-core</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0-m12</version>
</dependency>

并使用以下依赖项:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.17</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.17</version>
</dependency>

使用此uri方法,可在中的JerseyUriBuilder类中实现方法jersey- common

编辑

你需要改变,在你web.xml,的servlet
com.sun.jersey.spi.container.servlet.ServletContainerorg.glassfish.jersey.servlet.ServletContainerinit- paramcom.sun.jersey.config.property.packagesjersey.config.server.provider.packages

2020-09-08