我正在尝试访问返回多行、多列响应的 REST 端点,例如:
A1 B1 C1 A2 B2 C2 A3 B3 C3 ... ...
I'm currently using jersey-client to hit this endpoint and trying to look for the neatest way to parse this response. Here, each line would represent a bean, say MyBean
and each column on that would represent a property in that bean. The order of values in the response is always fixed.
I can get the response back as a long String, split it at line-feeds and tabs to get individual values.
However, I would like to know if there is a way where I can get the results as a List<String>
, where each element in the List
would represent a line of the response. I can then split it on \t
to get individual values.
Here's what I've tried:
WebResource resource = client.resource(NETSPEAK_URL)
.type(MediaType.TEXT_PLAIN)
.get(new GenericType<List<String>>(){});
但这会导致以下异常:
A message body reader for Java class java.util.List,
and Java type java.util.List<java.lang.String>,
and MIME media type text/plain; charset=UTF-8 was not found
如果我更加贪婪,我想知道是否可以获得映射到我的 bean MyBean
属性的各个列值。我考虑过围绕 MyBean
创建一个包装器,以获得一个 MyBean
列表,但是如何对其进行注释以协助解析呢?这对于 xml/json 响应来说是有意义的。但这是纯文本。
是否有可能以某种方式告诉 Jersey 客户端有关此文本/纯响应的解析?如果这可以通过 Apache HTTP 客户端实现,我就准备好了。
谢谢
最佳答案
您可能想要实现一个代表您的 bean 列表的类,例如 class BeanList extends List<Bean>
,并实现 MessageBodyReader<BeanList>
(参见 http://jsr311.java.net/nonav/releases/1.1/javax/ws/rs/ext/MessageBodyReader.html )教 Jersey 如何将字符串读取为 BeanList
.
然后你可以使用BeanList.class
而不是List<String>
作为 get
的参数称呼。
关于java - 使用 jersey/apache REST 客户端解析多行、多列文本响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11510570/