Java 在 maven 构建的 JAR 中找不到 Jersey 消息正文阅读器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2730589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:14:02  来源:igfitidea点击:

Jersey message body reader not found in maven-built JAR

javarestjax-rsexecutable-jarmaven-assembly-plugin

提问by Olvagor

My application uses a REST (JAX-RS Jersey) interface. When I run it in Eclipse, everything' s fine. The domain objects are annotated, I'm not using XML files for the REST mapping.

我的应用程序使用 REST (JAX-RS Jersey) 接口。当我在 Eclipse 中运行它时,一切正常。域对象已注释,我没有将 XML 文件用于 REST 映射。

Now I created a standalone JAR using the maven-assembly-plugin, which packs the application and all dependencies in a single, executable JAR file. This also seems to work.

现在我使用 maven-assembly-plugin 创建了一个独立的 JAR,它将应用程序和所有依赖项打包在一个可执行的 JAR 文件中。这似乎也有效。

But when I start the application and request an object from the server, Jersey complains, that it can't find a message body reader:

但是当我启动应用程序并从服务器请求一个对象时,Jersey 抱怨说它找不到消息正文阅读器:

com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java type, class de.rybu.atuin.core.entity.User, and MIME media type, application/json, was not found

Any ideas why this happens?

任何想法为什么会发生这种情况?

EDIT:After I slept a night over it, I noticed that it complains about JSON... but I'm using only XML for serialization. Strange.

编辑:在我睡了一夜之后,我注意到它抱怨 JSON...但我只使用 XML 进行序列化。奇怪的。

采纳答案by Olvagor

I fixed the problem and I guess I know how :-)

我解决了这个问题,我想我知道如何:-)

My resources were annotated like this:

我的资源是这样注释的:

@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path(CONTEXT_ADDRESS)
public class UserResource
{
}

My client used the reverse order:

我的客户使用了相反的顺序:

WebResource wr = ...
User user = wr.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE).get(new GenericType<User>(){});

I don't know what initially caused the problem but I completely removed JSON-support and now it works. Maybe it would have been sufficient to simply swith the order of JSON and XML in the client but I didn't try that.

我不知道最初是什么导致了这个问题,但我完全删除了 JSON 支持,现在它可以工作了。也许在客户端简单地改变 JSON 和 XML 的顺序就足够了,但我没有尝试过。

回答by bytesmith

I ran into a similar problem (worked fine running from eclipse or deployed as separate jars but not from executable jar) and found that this approachfor creating executable jars using the maven dependency plugin and maven jar plugin works properly. This is because it puts the dependencies in a separate lib directory and then includes that in the classpath in the manifest as opposed to merging them all together which can cause numerous issues.

我遇到了类似的问题(从 eclipse 运行正常或作为单独的 jar 部署但不是从可执行 jar 部署),并发现这种使用 maven 依赖插件和 maven jar 插件创建可执行 jar 的方法可以正常工作。这是因为它将依赖项放在单独的 lib 目录中,然后将其包含在清单的类路径中,而不是将它们全部合并在一起,这可能会导致许多问题。

回答by Tran Cay

I run into the same issue, searching on stackoverflow, I found that adding jersery-json-1.x.jar into WEB-INF/lib like suggested by this solutionwill solve the issue. Please give award to Mikhail!

我遇到了同样的问题,在 stackoverflow 上搜索,我发现将 jersery-json-1.x.jar 添加到 WEB-INF/lib 就像这个解决方案建议的那样 可以解决这个问题。请给米哈伊尔颁奖!

回答by Kisanagaram

I faced the same issue (http://goo.gl/Mk9sZ) . It got solved by changing maven dependency for jersey-multipart jar from 1.0.2 to 1.8 version (Used the same dependency in client side as well as provider side.

我遇到了同样的问题 ( http://goo.gl/Mk9sZ)。通过将 jersey-multipart jar 的 maven 依赖项从 1.0.2 更改为 1.8 版本(在客户端和提供程序端使用相同的依赖项)解决了这个问题。

             <dependency>
                <groupId>com.sun.jersey.contribs</groupId>
                <artifactId>jersey-multipart</artifactId>
                <version>1.8</version>
             </dependency>

You can find the complete code I used at http://goo.gl/Mk9sZ

您可以在http://goo.gl/Mk9sZ找到我使用的完整代码

回答by Filipe Ferreira

I am using Jersey Client 1 and to solve this problem, I created a generic json message body reader.

我正在使用 Jersey Client 1 并解决这个问题,我创建了一个通用的 json 消息正文阅读器。

public class JSONMessageBodyReader<T> implements MessageBodyReader<T> {

@Override
public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2,
        MediaType arg3) {
    return true;
}

@SuppressWarnings("unchecked")
@Override
public T readFrom(Class<T> clazz, Type type, Annotation[] arg2,
        MediaType arg3, MultivaluedMap<String, String> arg4,
        InputStream is) throws IOException, WebApplicationException {

    byte[] bytes = new byte[is.available()];
    is.read(bytes);
    String json = new String(bytes, "UTF-8");

    ObjectMapper mapper = new ObjectMapper();
    mapper.readValue(json, TypeFactory.defaultInstance().constructType(type));

    return (T) mapper.readValue(json, TypeFactory.defaultInstance().constructType(type));

}

}

}