如何在 REST Web 服务中使用 json 生成 http 响应?
我想在网络服务中使用 json 进行响应,如下所示:
HTTP/1.1 200 OK
Content-Type: application/vnd.org.snia.cdmi.dataobject+json
X-CDMI-Specification-Version: 1.0
{
"objectURI" : "/MyContainer/MyDataObject.txt",
"objectID" : "AABwbQAQb/ENV52Ai8a3MA==",
"parentURI" : "/MyContainer/",
"mimetype" : "text/plain",
"metadata" : {
"cdmi_size" : "17"
},
"valuerange" : "0-17",
"value" : "Hello CDMI World!"
}
但现在我只能显示
HTTP/1.1 200 OK
Content-Type: application/vnd.org.snia.cdmi.dataobject+json
X-CDMI-Specification-Version: 1.0
{
"objectURI" : "/MyContainer/MyDataObject.txt",
"objectID" : "AABwbQAQb/ENV52Ai8a3MA==",
"parentURI" : "/MyContainer/",
}
如何在 "mimetype"
之后放置 "meta" : {....}
从上面。以及如何获取 "meta"
作为 BasicDBObject 或其他类型?我使用 jersey 框架和 java 编写 Web 服务。
谢谢
I want to response using json in web service like this :
HTTP/1.1 200 OK
Content-Type: application/vnd.org.snia.cdmi.dataobject+json
X-CDMI-Specification-Version: 1.0
{
"objectURI" : "/MyContainer/MyDataObject.txt",
"objectID" : "AABwbQAQb/ENV52Ai8a3MA==",
"parentURI" : "/MyContainer/",
"mimetype" : "text/plain",
"metadata" : {
"cdmi_size" : "17"
},
"valuerange" : "0-17",
"value" : "Hello CDMI World!"
}
But now I can only show like
HTTP/1.1 200 OK
Content-Type: application/vnd.org.snia.cdmi.dataobject+json
X-CDMI-Specification-Version: 1.0
{
"objectURI" : "/MyContainer/MyDataObject.txt",
"objectID" : "AABwbQAQb/ENV52Ai8a3MA==",
"parentURI" : "/MyContainer/",
}
How to put "meta" : {....}
after "mimetype"
from above .And how to get the "meta"
as BasicDBObject or other types?I write web service using jersey framework and java.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Jersey 将对象序列化为 JSON 之前,您需要使用 JAXB Binding 注释来注释对象。下面是一个示例类,请注意,您可以使用 @XmlType 注释的 propOrder 属性对元素重新排序。还可以为 XML 元素指定方法名称以外的名称。
You will need to use JAXB Binding annotations to annotate your objects before Jersey serializes them to JSON. Below is an example class, note that you can reorder elements with the propOrder attribute of the @XmlType annotation. It is also possible to give XML elements names other than their method names.