如何在 REST Web 服务中使用 json 生成 http 响应?

发布于 2024-11-09 17:30:51 字数 870 浏览 0 评论 0原文

我想在网络服务中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2024-11-16 17:30:51

在 Jersey 将对象序列化为 JSON 之前,您需要使用 JAXB Binding 注释来注释对象。下面是一个示例类,请注意,您可以使用 @XmlType 注释的 propOrder 属性对元素重新排序。还可以为 XML 元素指定方法名称以外的名称。

@XmlRootElement
@XmlType(propOrder = { "name", "elements" })
public class ExampleObject implements Serializable {

    private static final long serialVersionUID = 1L;
    private Collection<String> elements = null;
    private String name = null;

    @XmlElementWrapper(name = "elements")
    @XmlElement(name = "element")
    public Collection<String> getElements() {
        return this.elements;
    }

    public String getName() {
        return this.name;
    }

    public void setElements(final Collection<String> elements) {
            this.elements = elements;
    }

    public void setName(final String name) {
            this.name = name;
    }

}

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.

@XmlRootElement
@XmlType(propOrder = { "name", "elements" })
public class ExampleObject implements Serializable {

    private static final long serialVersionUID = 1L;
    private Collection<String> elements = null;
    private String name = null;

    @XmlElementWrapper(name = "elements")
    @XmlElement(name = "element")
    public Collection<String> getElements() {
        return this.elements;
    }

    public String getName() {
        return this.name;
    }

    public void setElements(final Collection<String> elements) {
            this.elements = elements;
    }

    public void setName(final String name) {
            this.name = name;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文