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

发布于 2024-08-30 06:09:27 字数 539 浏览 13 评论 0原文

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

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

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

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

有什么想法为什么会发生这种情况吗?

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

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.

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.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

守护在此方 2024-09-06 06:09:27

我遇到了同样的问题,在 stackoverflow 上搜索,我发现将 jersery-json-1.x.jar 添加到 WEB-INF/lib 中,就像 此解决方案将解决该问题。
请给Mikhail颁奖!

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 solution will solve the issue.
Please give award to Mikhail!

挽梦忆笙歌 2024-09-06 06:09:27

我解决了这个问题,我想我知道怎么做:-)

我的资源是这样注释的:

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

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

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

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

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.

流云如水 2024-09-06 06:09:27

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

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 approach for 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.

反差帅 2024-09-06 06:09:27

我遇到了同样的问题(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>

您可以在 http://goo.gl/Mk9sZ

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.

             <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

萌能量女王 2024-09-06 06:09:27

我正在使用 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));

}

}

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

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));

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文