3

我按照这里的指南http://docs.neo4j.org/chunked/milestone/server-java-rest-client-example.html关于如何发送 POST 请求。使用下面的代码,我得到了错误java.lang.NoSuchMethodError: javax.ws.rs.ext.MessageBodyWriter.isWriteable,这似乎是我正在使用的库的问题。

public Object getResponse(Object responseClass) {
    Client client = Client.create();

    WebResource webResource = client.resource(getUrl());
    ClientResponse response = webResource
            .type(MediaType.APPLICATION_JSON)
            .entity(responseClass)
            .post(ClientResponse.class);

    if (response.getStatus() != 201)
        return null;

    return response.getEntity(responseClass.getClass());
}

我不确定我是否缺少依赖项或什么...

我的 pom.xml 包括

  <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-jaxrs</artifactId>
      <version>1.9.12</version>
  </dependency>
  <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>2.1.2</version>
  </dependency>

pom.xml

  <dependencies>
      <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.17</version>
      </dependency>
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.23</version>
      </dependency>
      <dependency>
          <groupId>org.apache.axis</groupId>
          <artifactId>axis</artifactId>
          <version>1.4</version>
      </dependency>
      <dependency>
          <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          <version>1.1.1</version>
      </dependency>
      <dependency>
          <groupId>commons-discovery</groupId>
          <artifactId>commons-discovery</artifactId>
          <version>0.4</version>
      </dependency>
      <dependency>
          <groupId>javax.xml</groupId>
          <artifactId>jaxrpc-api</artifactId>
          <version>1.1</version>
      </dependency>
      <dependency>
          <groupId>wsdl4j</groupId>
          <artifactId>wsdl4j</artifactId>
          <version>1.6.2</version>
      </dependency>
      <dependency>
          <groupId>javax.mail</groupId>
          <artifactId>mail</artifactId>
          <version>1.4.4</version>
      </dependency>
      <dependency>
          <groupId>javax.activation</groupId>
          <artifactId>activation</artifactId>
          <version>1.1.1</version>
      </dependency>
      <dependency>
          <groupId>org.apache.commons</groupId>
          <artifactId>commons-lang3</artifactId>
          <version>3.1</version>
      </dependency>
      <dependency>
          <groupId>org.apache.cxf</groupId>
          <artifactId>cxf-rt-frontend-jaxrs</artifactId>
          <version>2.1.2</version>
      </dependency>
      <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.2</version>
      </dependency>

      <!-- Jersey & Jackson -->
      <dependency>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-jaxrs</artifactId>
          <version>1.9.12</version>
      </dependency>
      <dependency>
          <groupId>org.codehaus.jackson</groupId>
          <artifactId>jackson-xc</artifactId>
          <version>1.9.12</version>
      </dependency>
      <dependency>
          <groupId>com.sun.jersey</groupId>
          <artifactId>jersey-server</artifactId>
          <version>1.17</version>
      </dependency>
      <dependency>
          <groupId>com.sun.jersey</groupId>
          <artifactId>jersey-json</artifactId>
          <version>1.17</version>
      </dependency>
      <dependency>
          <groupId>com.sun.jersey</groupId>
          <artifactId>jersey-servlet</artifactId>
          <version>1.17</version>
      </dependency>
      <dependency>
          <groupId>com.sun.jersey</groupId>
          <artifactId>jersey-grizzly2</artifactId>
          <version>1.17</version>
      </dependency>
      <dependency>
          <groupId>com.sun.jersey</groupId>
          <artifactId>jersey-client</artifactId>
          <version>1.17.1</version>
      </dependency>

      <!-- Spring, Hibernate -->
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>4.1.9.Final</version>
      </dependency>
      <dependency>
          <groupId>org.hibernate.javax.persistence</groupId>
          <artifactId>hibernate-jpa-2.0-api</artifactId>
          <version>1.0.1.Final</version>
      </dependency>
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-entitymanager</artifactId>
          <version>4.1.9.Final</version>
      </dependency>
      <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-annotations</artifactId>
          <version>3.5.6-Final</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>3.2.1.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>3.2.1.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-jpa</artifactId>
          <version>1.2.0.RELEASE</version>
      </dependency>
  </dependencies>

更新 删除以下条目修复了它

  <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxrs</artifactId>
      <version>2.1.2</version>
  </dependency>
4

1 回答 1

3

您的类路径中似乎有不同的 javax.ws.rs.ext.MessageBodyWriter 类。一个来自 jsr311-api-0.8.jar,另一个来自 jersey-core-1.17.jar。所以请谨慎管理依赖关系。

顺便说一句,如果您使用JFind(在目录/JAR 中查找类)和jd-gui(反编译 JAR)等工具,您可以轻松解决此类问题。

以下是我的 JFind 输出:

ClassName = javax/ws/rs/ext/MessageBodyWriter.class
JarName = C:\Users\lhuang\.m2\repository\com\sun\jersey\jersey-core\1.17\jersey-core-1.17.jar
----------------
ClassName = javax/ws/rs/ext/MessageBodyWriter.class
JarName = C:\Users\lhuang\.m2\repository\javax\ws\rs\jsr311-api\0.8\jsr311-api-0.8.jar
----------------
于 2013-03-14T16:29:51.830 回答