无法让resteasy客户端工作

时间:2017-05-08 17:45:43

标签: rest maven jboss resteasy

我尝试了许多不同的方法让resteasy客户端为我工作,但我遇到了错误。 java.lang.LinkageError:无法链接org / jboss / resteasy / client / jaxrs / internal / ClientConfiguration

以下是我用作客户端的方法

    public void createIssue() {
    issue = new Issue();
    ResteasyClient client = new ResteasyClientBuilder().build();
    ResteasyWebTarget target = client.target("api-being-posted-to");
    Response response = target.request().header("Authorization: ", "Basic Foo")
            .post(Entity.entity(issueJson, "application/json"));
}

我可以使用相同的数据进行POST和GET,并且我尝试使客户端使用不同的数据并发布到不同的地方并始终得到相同的错误。这是我的pom:

<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>org.foo.bar</groupId>
<artifactId>foo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>example</name>

<dependencies>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.1.0.Final</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <version>3.1.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>jaxrs-api</artifactId>
        <version>3.0.8.Final</version>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson-provider</artifactId>
        <version>3.1.0.Final</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
        </plugin>
    </plugins>
</build>

这是我的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">

<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/rest</param-value>
</context-param>

<listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
        </listener-class>
</listener>

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
</servlet>

如果有帮助,我正在使用JBoss EAP 6.4。

1 个答案:

答案 0 :(得分:0)

  1. 据我所知,JBoss EAP 6.4中提供的resteasy版本是3.0.21.Final而不是3.1.0.Final,但您可能已对其进行了修改(请参阅https://developer.jboss.org/thread/262213以供参考)

  2. 您为resteasy库使用了不同的版本和不同的范围(对于jaxrs-api为3.0.8.Final),但这可能仍然没问题。

  3. 我总是建议使用Resteasy Proxy Framework。可能需要更多的代码来编写,但总体上更简单,更清晰:

  4. 定义界面:

    public interface IssueIF
    {
       @POST
       @Path("/issue")
       @Produces("text/plain")
       Issue postIssue(Issue i);
    }
    

    使用它:

    ResteasyClient client = new ResteasyClientBuilder().build();
    ResteasyWebTarget target = client.target("http://example.com/base/uri");
    
    SimpleClient simple = target.proxy(IssueIF.class);
    Issue issue = new Issue();
    client.postIssue(issue);
    

    我没有编译这段代码,它主要是从文档中复制/粘贴的,但它应该接近你需要的。

    编辑:

    来自docs

      

    LinkageError的子类表明一个类有一些依赖性   在另一个班级;然而,后一类却发生了不相干的变化   在编写前一课后。

    这表明您在编译时和运行时使用的库版本存在问题,因此请参阅上面的1号。

    特别是如果使用不同的范围,则编译和运行时的版本必须相同!因此,请尝试找出JBoss提供的版本,并仅使用这些版本。还要找出提供的引用库,并在提供范围的情况下标记所有库。

相关问题