我的第一个Web客户端给出了“找不到媒体类型的MessageBodyWriter ...”。 (JSON) [英] My first web client giving "MessageBodyWriter not found for media type..." (JSON)

查看:448
本文介绍了我的第一个Web客户端给出了“找不到媒体类型的MessageBodyWriter ...”。 (JSON)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我先给你讲一个简短的故事。如果我在Eclipse中导出我的jar文件和jar文件(Runnable Jar向导并选择Package required libraries ...)而不是Extract required libraries,那么每件事都有效,但开始的速度极慢。如果我使用提取方法构建我的jar,代码将导致最终

I'll give you the short story first. If I, in Eclipse, export my jar file with jar files (the Runnable Jar wizard and select "Package required libraries...") instead of "Extract required libraries", every thing works, but it is excruciatingly slow to get going. If I build my jar with the extracting method the code will cause an eventual

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json

我没有使用Maven。我还不知道。在大多数情况下,我对Eclipse缺乏经验,但我到了那里。我认为Genson会给我创建JSON所需的东西。

I'm not using Maven. I don't know it yet. I am mostly inexperienced with Eclipse for the most part, but I am getting there. I thought Genson would give me what I need to create JSON.

如果我列出我的jar文件和grep for MessageBodyWriter,我得到

If I list my jar file and grep for MessageBodyWriter, I get

1220 Tue Nov 15 10:29:02 CST 2016 javax/ws/rs/ext/MessageBodyWriter.class             
  47 Tue Nov 15 10:29:02 CST 2016 META-INF/services/javax.ws.rs.ext.MessageBodyWriter 

要到达更长的故事,我在Eclipse中创建了一个简单的Java项目,将外部jar添加到我的构建路径中。 Eclipse不会抱怨任何遗漏。在我的代码中只有一两个关于未使用的this-and-thats的警告。出口工作正常,无论我做什么,没有问题。只有执行才会显示出一些刺激。

To get to the longer story, I created a simple Java project in Eclipse, added external jars to my build path. Eclipse doesn't complain about anything missing. There is only one or two warnings about unused this-and-thats in my code. The export works fine either way I do it, no problems. Only execution reveals some irritation.

在我的请求类中,我相信我已经正确设置了所有内容。这是可以想象的最简单的POST客户端。我有必要的空构造函数,正确的注释。

In my request class, I believe I have set up everything correctly. This is the simplest POST client imaginable. I have the requisite empty constructor, the correct annotations.

@XmlRootElement
@Produces(MediaType.APPLICATION_JSON)

public class QQNotificationRequest {

    @JsonProperty("id")
    private int id;
    @JsonProperty("subId")
    private int subId;
    @JsonProperty("type")
    private int type;
    @JsonProperty("scode")
    private int scode;
    @JsonProperty("datetime")
    private String datetime;
    ...

这是我的最终通话失败

Response resp = m_webTarget.request(MediaType.APPLICATION_JSON).post(Entity.json(message));

这些是我的外部罐子

javax.ws.rs-api-2.0.1.jar
aopalliance-repackaged-2.5.0-b05.jar
hk2-api-2.5.0-b05.jar
hk2-locator-2.5.0-b05.jar
hk2-utils-2.5.0-b05.jar
javassist-3.20.0-GA.jar
javax.annotation-api-1.2.jar
javax.inject-2.5.0-b05.jar
javax.servlet-api-3.0.1.jar
jaxb-api-2.2.7.jar
jersey-guava-2.24.jar
org.osgi.core-4.2.0.jar
osgi-resource-locator-1.0.1.jar
persistence-api-1.0.jar
validation-api-1.1.0.Final.jar
jersey-client.jar
jersey-common.jar
jersey-container-servlet.jar
jersey-container-servlet-core.jar
jersey-media-jaxb.jar
jersey-server.jar
jargs.jar
genson-1.4.jar
jt400.jar
log4j-api-2.7.jar
log4j-core-2.7.jar

我觉得罐子里有东西没有被正确导出,但我不确定。只是在这里猜测,因为Java必须经历爆炸大罐内所有罐子的过程,它才能找到开始执行所需的东西,而大罐子里面的.classes却没有。

I feel like there is something in the jar that isn't being 'exported' correctly, but I'm not sure. Just guessing here that because Java has to go through the process of exploding all of the jars inside the big jar, that it finds what it needs to begin execution, whereas with the .classes inside the big jar, it doesn't.

暂时失去了,但是仍在继续。我很感激任何指针。

Lost for the moment, but plugging along. I appreciate any pointers.

推荐答案

问题是 META-INF / services中有文件每个Jersey jar的目录,需要在运行时出现。这些文件用于自动加载和注册提供程序类。

The problem is that there are files in the META-INF/services directory of each Jersey jar, that need to be present at runtime. These files are used to automatically load and register provider classes.

当所有jar文件保持独立并且只是添加到类路径时,所有文件都保持完好。但是当您创建单个jar时,这些文件会重叠(它们都具有相同的名称),因此将只包含每个具有相同名称的文件中的一个。因此Genson的文件可能不是包含的文件。

When all the jars are kept separate and just add to the classpath, all the files remain in tact. But when you create a single jar, those files overlap (they all have the same name), so only one of each file with the same name will be included. So the file with Genson is probably not the one included.

您可以将Genson明确注册为应用程序的提供者。我不是真的使用Genson,所以我不确定这样做的正确方法。但快速浏览代码库,看起来你需要注册 GensonJsonConverter

You could just explicitly register Genson as a provider with the application. I don't really use Genson, so I'm not sure the correct way to do this. But quickly looking through the codebase, it looks like you need to register the GensonJsonConverter.

这可能有用,但是它没有解决没有包含服务文件的主要问题。可能还有其他未注册的automzatically注册组件。

This may work, but it doesn't solve the main problem about the services files not being included. There may be other automzatically registered components that aren't being registered.

我不是真正的Eclipse用户,所以我不知道如何解决这个问题。我知道如果你使用Maven,你会使用 maven-shade-plugin ,如上所述此处。这个插件的功能是将所有文件内容连接成一个文件。这样可以解决问题。

I'm not really an Eclipse user, so I'm not sure how to fix this problem. I know that if you are using Maven, you would use the maven-shade-plugin, as mentioned here. What this plugin would do is concatenate all the files content into one single file. This would solve the problem.

所以我想这只是答案的一半,因为我不确定在使用Eclipse时如何做类似的事情。但至少你现在知道这个问题。

So I guess this is only really half the answer, since I am not sure how to do something similar when using Eclipse. But at least you know the problem now.

FYI,此文件,是您主要关注的文件。如果你浏览其他泽西岛罐子,你会发现有类似的文件。这就是我所说的,当我说只使用一个时。在构建jar时需要找到一种方法将它们的内容连接成一个文件

FYI, this file, is the one you are mainly concerned about. If you look through other Jersey jars, you will see that there are similar files. This is what I'm talking about when I say only one will be used. You need to find a way to concatenate their contents into one file when you build the jar

这篇关于我的第一个Web客户端给出了“找不到媒体类型的MessageBodyWriter ...”。 (JSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆