创建文字/纯色泽西岛回复 [英] Create a text/plain Jersey response
问题描述
我有一些有效的代码,但是我正在寻找一种更好的方法来实现.我有一个RESTful Web API,我想支持JSON,XML和TEXT媒体类型.使用带有JAXB注释的"bean"类,可以轻松实现JSON和XML.我只需要 text/plain 即可工作,但我希望Jersey更加智能,并且能够使用toString
将我的咖啡豆列表List<Machine>
转换为字符串.>
这是Resource类. JSON和XML媒体类型使用带有JAXB注释的bean类.纯文本使用自定义字符串格式(基本上是命令的标准输出表示形式).
@Path("/machines")
public class MachineResource {
private final MachineManager manager;
@Inject
public MachineResource(MachineManager manager) {
this.manager = manager;
}
@GET @Path("details/")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public List<Machine> details() {
return manager.details();
}
@GET @Path("details/")
@Produces({ MediaType.TEXT_PLAIN })
public String detailsText() {
StringBuilder text = new StringBuilder();
for(Machine machine : manager.details()) {
text.append(machine.toString());
}
return text.toString();
}
是否有更好的方法,将Jersey自动转换为字符串,所以我只需要在这里实现一种方法?(可以处理所有3种媒体类型)
我看到我可以实现 MessageBodyWriter ,但这似乎麻烦很多.
如果有关系,我正在使用嵌入式的Jetty和Jersey选项.
谢谢!
实施MessageBodyReader
/Writer
是您需要执行的操作,以便执行以下操作:
@GET @Path("details/")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN })
public List<Machine> details() {
return manager.details();
}
要编写的代码不是很多,如果您能够足够通用地编写代码,则可以从中获得一些重用.
I have some code that works, but I am looking for a better way to do it. I have a RESTful web API that I want to support JSON, XML, and TEXT media types. The JSON and XML is easy with a JAXB annotated "bean" class. I just got text/plain to work, but I wish Jersey was a little more intelligent and would be able to convert a list of my beans, List<Machine>
to a string using toString
.
Here is the Resource class. The JSON and XML media types use a JAXB annotated bean class. The text plain uses a custom string format (basically the stdout representation of a command).
@Path("/machines")
public class MachineResource {
private final MachineManager manager;
@Inject
public MachineResource(MachineManager manager) {
this.manager = manager;
}
@GET @Path("details/")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public List<Machine> details() {
return manager.details();
}
@GET @Path("details/")
@Produces({ MediaType.TEXT_PLAIN })
public String detailsText() {
StringBuilder text = new StringBuilder();
for(Machine machine : manager.details()) {
text.append(machine.toString());
}
return text.toString();
}
Is there a better way to do this with Jersey automatically converting to a string, so I only have to implement one method here? (That can handle all 3 media types)
I see that I can implement a MessageBodyWriter, but that seems like a lot more trouble.
EDIT:
If it matters, I am using the embedded Jetty and Jersey options.
Thanks!
Implementing a MessageBodyReader
/Writer
would be what you need to do in order to do the following:
@GET @Path("details/")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_PLAIN })
public List<Machine> details() {
return manager.details();
}
It's not very much code to write, and if you are able to write it generic enough you will be able to get some re-use out of it.
这篇关于创建文字/纯色泽西岛回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!