有没有办法使用 javax.xml 来包装根元素?

发布于 2024-12-17 00:37:07 字数 1777 浏览 1 评论 0原文

我有一份杰里西申请。我有几个资源使用带有 javax.xml 注释的提供者注释的 bean,因此结果以 xml 或 json 形式返回,具体取决于接受标头中发送的内容(默认为 xml)。一切都很好。现在,我需要为每个响应添加一个根元素。

下面是一个示例 Provider Bean,资源使用数据设置它并返回结果。

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "error")
public class ErrorProvider {


private String errorCode;
private String errorMessage;

public ErrorProvider(){}

public ErrorProvider(final String errorCode,final String errorMessage){
    setErrorCode(errorCode);
    setErrorMessage(errorMessage);
}

@XmlAttribute(name = "number")
public String getErrorCode() {
    return errorCode;
}

public void setErrorCode(final String errorCode) {
    this.errorCode = errorCode;
}

@XmlElement
public String getErrorMessage() {
    return errorMessage;
}

public void setErrorMessage(final String errorMessage) {
    this.errorMessage = errorMessage;
}
}

资源方法上的 Produces 注释是:

@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})

因此,除非在请求的接受标头中定义了 json,否则默认返回为 XML 格式。就像我说的,这很好用。它返回这样的内容:

<error code="100">
    <errorMessage>An error occurred</errorMessage>
</error>

好的,背景介绍完了,现在问问题。我需要向所有这些提供程序添加一个根元素,这对于所有提供程序都是相同的。所以它看起来像这样:

<transaction>
    <status>ok</status>
    <error code="100">
        <errorMessage>An error occurred</errorMessage>
    </error>
</transaction>

在类级别拥有类似 @XmlElementWrapper 的东西会很酷。我尝试创建另一个用 javax.xml 注释的 bean 并尝试扩展它,尝试做一个通用类型 bean。我就是想不通。我能做的最好的事情就是将事务注释放在错误 XML 中,就像它是另一个元素一样。我知道我可以编辑每个提供程序 bean 以包含事务根元素,但我认为应该有一种方法可以创建一次并应用于每个提供程序 bean。

I have a Jeresey application. I have several resources that use beans annotated with javax.xml annotations for providers so the result returns in xml or json depending on what is sent in the accept header with xml being the default. All that works fine. Now, I need to add a root element to every response.

Here is a sample Provider Bean that the resource sets with data and returns the result.

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "error")
public class ErrorProvider {


private String errorCode;
private String errorMessage;

public ErrorProvider(){}

public ErrorProvider(final String errorCode,final String errorMessage){
    setErrorCode(errorCode);
    setErrorMessage(errorMessage);
}

@XmlAttribute(name = "number")
public String getErrorCode() {
    return errorCode;
}

public void setErrorCode(final String errorCode) {
    this.errorCode = errorCode;
}

@XmlElement
public String getErrorMessage() {
    return errorMessage;
}

public void setErrorMessage(final String errorMessage) {
    this.errorMessage = errorMessage;
}
}

The produces annotation on the resource method is:

@Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})

So the default return is in XML unless json is defined in the accept header of the request. Like I said, this works fine. It returns something like this:

<error code="100">
    <errorMessage>An error occurred</errorMessage>
</error>

OK, enough with the backdrop, now with the question. I need to add a root element to all of these providers, which will be the same for all. So it would look like this:

<transaction>
    <status>ok</status>
    <error code="100">
        <errorMessage>An error occurred</errorMessage>
    </error>
</transaction>

It would be cool to have something like an @XmlElementWrapper at the class level. I tried creating another bean annotated with the javax.xml and tried to extend it, tried to do a generic type bean. I just can't figure it out. The best thing I was able to do is to have the transaction annotation inside the error XML like it was another element. I know I could edit each provider bean to include the transaction root element, but I think there should be a way to create that once and apply to every provider bean.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

假情假意假温柔 2024-12-24 00:37:07

您可以添加一个 ContainerResponseFilter 来使用 Transaction 对象包装您的响应。这就是它的样子(未经测试):

public class MyFilter implements ContainerResponseFilter {
    @Override
    public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
        response.setEntity(new Transaction(response.getEntity()));
        return response;
    }
}

您可以在此处找到有关如何注册过滤器的更多信息:http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/container/filter/package-summary.html

You could add a ContainerResponseFilter that wraps your responses using the Transaction object. This is how it could look like (untested):

public class MyFilter implements ContainerResponseFilter {
    @Override
    public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
        response.setEntity(new Transaction(response.getEntity()));
        return response;
    }
}

You can find out more about how to register filters here: http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/container/filter/package-summary.html

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