在 JAXB 中创建 SOAP 属性

发布于 11-02 20:25 字数 1733 浏览 5 评论 0原文

我目前正在学习JAXB和Web Service,我有这个我不知道的问题 怎么解决。

假设我有一个用 JAXB 注释的非常简单的类。

@XmlRootElement
public class Customer {
    private int custID;
    private String custName;
    //getters and setters
}

我将这个类公开为 Web 服务。 (注意:为了简单起见,我在这里对所有内容进行了硬编码 但这连接到数据库)

@WebService
public class CustomerWS {

    @WebMethod(operationName = "customerData")
    public Customer getCustomer(){
      Customer cust = new Customer();
      cust.setCustID(12345);
      cust.setCustName("John Doe");     
      return cust;
    }
}

SOAP 信封响应是这样的。

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:customerDataResponse xmlns:ns2="http://test.com/">
         <return>
            <custID>12345</custID>
            <custName>John Doe</custName>
         </return>
      </ns2:customerDataResponse>
   </S:Body>
</S:Envelope>

现在假设我在客户实体中有另一个称为状态的属性,他们希望此属性作为肥皂的属性 响应如下所示,而不是成为客户元素的一部分。 (A = 活动,I = 不活动)

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:customerDataResponse status="A" xmlns:ns2="http://test.com/">
         <return>
            <custID>12345</custID>
            <custName>John Doe</custName>
         </return>
      </ns2:customerDataResponse>
   </S:Body>
</S:Envelope>

@XmlRootElement
public class Customer {
    private int custID;
    private String custName;

    //Another Annotation??? (A or I only)
    private String status;
    //getters and setters
}

我如何注释我的类以满足此要求?谢谢

I am currently learning JAXB and Web Service and I have this problem that I dont know
how to solve.

Suppose I have this very simple class that I annotated with JAXB.

@XmlRootElement
public class Customer {
    private int custID;
    private String custName;
    //getters and setters
}

And I have this class that I am exposing as a Web Service. (Note: I hardcode everything here for simplicity
but this connects to DB)

@WebService
public class CustomerWS {

    @WebMethod(operationName = "customerData")
    public Customer getCustomer(){
      Customer cust = new Customer();
      cust.setCustID(12345);
      cust.setCustName("John Doe");     
      return cust;
    }
}

The SOAP envelope response is like this.

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:customerDataResponse xmlns:ns2="http://test.com/">
         <return>
            <custID>12345</custID>
            <custName>John Doe</custName>
         </return>
      </ns2:customerDataResponse>
   </S:Body>
</S:Envelope>

Now suppose I have another property in the customer entity called status and they wanted this property as attribute to the soap
response to be like below instead of being part of the customer element. (A = Active, I = Inactive)

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:customerDataResponse status="A" xmlns:ns2="http://test.com/">
         <return>
            <custID>12345</custID>
            <custName>John Doe</custName>
         </return>
      </ns2:customerDataResponse>
   </S:Body>
</S:Envelope>

@XmlRootElement
public class Customer {
    private int custID;
    private String custName;

    //Another Annotation??? (A or I only)
    private String status;
    //getters and setters
}

How can I annotate my class to satisfy this requirement? Thanks

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

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

发布评论

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

评论(1

巴黎夜雨2024-11-09 20:25:58

Customer 类上注释的所有内容都将与 customer 元素相关。

这是因为 JAX-WS 负责形成消息信封,然后 JAXB 将消息正文编组到该信封中。当 JAXB 整理主体时,更改信封已为时已晚。

Everything annotated on the Customer class will be relative to the customer element.

This is because JAX-WS is responsible for forming the message envelope, and then JAXB marshals the body of the message into this envelope. When it comes time for JAXB to marshal the body it is too late to change the envelope.

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