【问题标题】:How can I configure the target namespace globally in JAX-WS web services?如何在 JAX-WS Web 服务中全局配置目标命名空间?
【发布时间】:2013-06-04 10:52:01
【问题描述】:

我有很多用@WebService(targetNamespace = "mynamespace") 注释的端点。每个@WebResult@WebParam 都有相同的targetNamespace = "mynamespace" 定义。

有没有办法将 JAX-WS(Metro 实现)配置为默认使用 "mynamespace" 作为 targetNamespace?

我想使用没有任何属性的注解并摆脱重复的声明,就像约定优于配置一样。

【问题讨论】:

    标签: jax-ws java-metro-framework convention-over-configur


    【解决方案1】:

    只将targetNamespace放在服务端点接口服务实现中 豆.

    /**
    * Annotated Implementation Object
    */
    @WebService(
        name = "CustomerService",
        targetNamespace = "http://org.company.services"
    )
    public class CustomerService {
        @WebMethod
        @WebResult(name="CustomerRecord")
        public CustomerRecord locateCustomer(
            @WebParam(name="FirstName") String firstName,
            @WebParam(name="LastName") String lastName,
            @WebParam(name="Address") USAddress addr) {
            ...
        }
    };
    

    如果@WebResult@WebParam 没有targetNamespace,则默认为 targetNamespace 为 网络服务。

    另一方面,如果您不需要使用 JAX-B 进行自定义,则可以避免使用所有注释并仅使用 @WebService

    JSR-181 Web Services Metadata for the JavaTM Platform中查看更多信息

    【讨论】:

    • 当我在@WebResult@WebParam 省略了targetNamespace 时,SOAP 响应中生成的 XML 具有用于最外层 XML 标记(例如 <ns0:locateCustomerResponse>)的命名空间,但内部那些使用默认命名空间(例如<CustomerRecord>)。参数也是如此(<firstName> 而不是<ns0:firstName>)。
    • 有人会认为上面的例子可行,但不幸的是它不喜欢上面提到的 tim。为什么这么简单的事情不容易实现?必须有一种方法可以避免在每个 WebParam 和 WebResult 注释上不断复制和粘贴此命名空间。
    • 似乎最简单的方法是重命名 java 包以匹配 targetNamespace :(
    猜你喜欢
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多