简体   繁体   English

从球衣响应强制json

[英]force json from jersey response

I am trying to implement this example . 我正在尝试实现此示例 The first method in FileResource is supposed to return a json response, but the response is coming through as XML. FileResource中的第一个方法应该返回json响应,但是响应以XML的形式通过。 I have never used jersey before. 我以前从未用过球衣。 I tried adding @produces yielding this: 我尝试添加@produces产生此:

@GET @Produces("application/json")
@Path("/url")
public Response getCallbackUrl() {
    String url = blobstoreService.createUploadUrl("/rest/file");
    return Response.ok(new FileUrl(url)).build();
}

Now I'm getting 现在我得到

A message body writer for Java class FileUrl, and Java type class FileUrl, and MIME media type application/json was not found

How can I fix this? 我怎样才能解决这个问题? Thanks for any help. 谢谢你的帮助。 And if you need any more info let me know. 如果您需要更多信息,请告诉我。 I don't know much of the jersey terminology. 我不太了解球衣术语。

You need a few more dependencies in order to produce JSON output. 您还需要一些依赖关系才能生成JSON输出。

Take a look at: http://jersey.java.net/nonav/documentation/latest/chapter_deps.html 看看: http : //jersey.java.net/nonav/documentation/latest/chapter_deps.html

Scroll down to: 向下滚动到:

11.4.1.2. 11.4.1.2。 MOXy Maven developers, using JSON serialization support of JAXB beans when using the MIME media type application/json require a dependency on the jersey-json module (explicit dependency on org.eclipse.persistence.moxy is required). MOXy Maven开发人员在使用MIME媒体类型application / json时使用JAXB bean的JSON序列化支持需要对jersey-json模块的依赖(需要对org.eclipse.persistence.moxy的显式依赖)。


An alternative to "vanilla" Jersey that you might be interested in is Dropwizard . 您可能会感兴趣的“香草”球衣的替代品是Dropwizard Their blurb is: 他们的内容是:

Dropwizard has out-of-the-box support for sophisticated configuration, application metrics, logging, operational tools, and much more, allowing you and your team to ship a production-quality HTTP+JSON web service in the shortest time possible. Dropwizard对复杂的配置,应用程序指标,日志记录,操作工具等提供了开箱即用的支持,使您和您的团队可以在最短的时间内交付生产质量的HTTP + JSON Web服务。

Yep you'll need some jersey / json dependency here : 是的,您在这里需要一些jersey / json依赖项:

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.2</version>
</dependency>

with your version of jersey of course. 当然还有您的球衣版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM