【问题标题】:Create SOAP Attribute in JAXB在 JAXB 中创建 SOAP 属性
【发布时间】:2011-04-22 09:08:06
【问题描述】:

我目前正在学习 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>

现在假设我在客户实体中有另一个名为 status 的属性,他们希望这个属性作为肥皂的属性 响应如下所示,而不是成为客户元素的一部分。 (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
}

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

【问题讨论】:

    标签: java web-services soap jaxb jax-ws


    【解决方案1】:

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

    这是因为 JAX-WS 负责形成消息信封,然后 JAXB 将消息体编组到这个信封中。当 JAXB 对主体进行编组时,更改信封为时已晚。

    【讨论】:

    • 感谢您的回复。是否有任何替代方案或解决方法,以便我可以做我想做的事?另外,有没有一种方法可以将任何属性硬编码到信封中,即使它不属于任何实体(只需简单硬编码任何实体..)?
    猜你喜欢
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    相关资源
    最近更新 更多