java - 仅当执行 jar 文件时,REST 客户端中出现 ClientHandlerException

标签 java rest maven jar

我已经设置了 REST Web 服务和客户端。 Web 服务部署在 Glassfish 服务器上。如果我从 netbeans 运行客户端的主类,一切都会正常。现在我使用 Maven 创建一个可执行的 jar 文件,如果我尝试运行这个 jar 文件,我会收到以下异常消息:

21.07.2013 13:40:05 com.sun.jersey.api.client.ClientResponse getEntity
SCHWERWIEGEND: A message body reader for Java class java.lang.String, and Java type class java.lang.String, and MIME media type application/xml was not found
21.07.2013 13:40:05 com.sun.jersey.api.client.ClientResponse getEntity
SCHWERWIEGEND: The registered message body readers compatible with the MIME media type are:
*/* ->
  com.sun.jersey.atom.rome.impl.provider.entity.AtomFeedProvider
  com.sun.jersey.atom.rome.impl.provider.entity.AtomEntryProvider

Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class java.lang.String, and Java type class java.lang.String, and MIME media type application/xml was not found
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:561)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:517)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:684)
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
    at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:507)
    at de.movienexuscmd.MovieNexusWebserviceClient.getXml(MovieNexusWebserviceClient.java:52)
    at de.movienexuscmd.App.run(App.java:20)
    at de.movienexuscmd.App.main(App.java:16)

现在我真的不知道为什么只有在尝试执行jar文件时才会出现这个异常。希望任何人都可以提供帮助。

我的网络服务如下所示:

@Path("MovieNexus")
public class MovieNexusWebService {

@Context
private UriInfo context;

private MovieNexusFrontController frontController = null;

/**
 * Creates a new instance of MovieNexusWebService
 */
public MovieNexusWebService() {
    frontController = new MovieNexusFrontController();
}

/**
 * Retrieves representation of an instance of
 * de.webservice.MovieNexusWebService
 *
 * @return an instance of java.lang.String
 */
@GET
@Produces("application/xml")
public String getXml() {
    return frontController.getRandomMovie().getMovieName();
}

/**
 * PUT method for updating or creating an instance of MovieNexusWebService
 *
 * @param content representation for the resource
 * @return an HTTP response with content of the updated or created resource.
 */
@PUT
@Consumes("application/xml")
public void putXml(String content) {
}
}

客户:

public class MovieNexusWebserviceClient {

private WebResource webResource;

private Client client;

private static final String BASE_URI = "http://localhost:17589/MovieNexusWebservice/webresources";

public MovieNexusWebserviceClient() {
    com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
    client = Client.create(config);
    webResource = client.resource(BASE_URI).path("MovieNexus");
}

public String getXml() throws UniformInterfaceException {
    WebResource resource = webResource;
    return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(String.class);
}

public void putXml(Object requestEntity) throws UniformInterfaceException {
    webResource.type(javax.ws.rs.core.MediaType.APPLICATION_XML).put(requestEntity);
}

public void close() {
    client.destroy();
}
}

问候

诺亚

最佳答案

经过几个小时的思考和尝试,我自己解决了这个问题。问题是由 maven 创建可执行 jar。其中缺少一些依赖项,因此我更改了插件配置:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                                 <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>mainclass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

关于java - 仅当执行 jar 文件时,REST 客户端中出现 ClientHandlerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17772280/

相关文章:

java - 让代码更加函数式风格

java - 从 java.util.Date 到 java.sql.Timestamp 时添加了无法解释的零

java - 删除JTable的列数

javascript - req.body.content 给出 undefined - Express JS

mysql - NodeJS + MySQL 登录 API、客户端 session

java - import org.apache.poi.xssf 无法解析

java - 如何使用单一连接管理 DAO

apache-flex - 将 crossdomain.xml 添加到 tomcat 6.0.24 以使用 Flex 访问 Web 服务

java - 寻找基于终端的 IDE 以通过 SSH 工作 - Java

maven - 限制在 SNAPSHOT 依赖项上触发哪些下游构建