CXF/Spring将顶级服务名称和命名空间注入JAX-WS接口



我正在运行一个基于CXF的Web服务,与CXF网站上的示例并不相似http://cxf.apache.org/docs/jax-ws-configuration.html。该服务有一个基于以下示例上下文的实现端点:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd">
    <jaxws:endpoint id="myServiceMembersImpl"
        implementor="#myService"
        endpointName="e:MyServiceMembersEndpoint"
        serviceName="s:MyServiceMembersService"
        address="http://localhost:8080/myservicemembers"
        xmlns:e="http://localhost:8080/myservicemembers/ns"
        xmlns:s="http://localhost:8080/myservicemembers/ns"/>
</beans>

当然,还有Java。。。

接口:

package com.me.service;
@WebService
public interface MyService {
String MEMBER = "MEMBER";
@WebResult(name = MEMBER)
Member getMember(@WebParam(name = "memberId") long memberId) throws Exception;
     // ... 
     // more interface declarations 
     // ... 
} // end interface 

以及,实现:

package com.me.service.impl;
@WebService(endpointInterface = "com.me.service.MyService")
@Path("/")
public class MyServiceMembersImpl implements MyService {
@GET
@Path("/{id}")
@Consumes({ APP_JSON, APP_XML })
@Produces({ APP_JSON, APP_XML })
@Transactional(readOnly = true, propagation = Propagation.REQUIRED)
public Member getMember(@PathParam("id") final long memberId) throws Exception {
        // ... 
        // business logic 
        // ... 
        return theMember; 
     } // end method 
} // end class 

它返回一个类似于以下开头的WSDL:

<?xml version="1.0" encoding="UTF-8" ?> 
<wsdl:definitions name="MyServiceImplService" 
    targetNamespace="http://localhost:8080/myservicemembers/ns" 
    xmlns:ns1="**http://service.me.com/**" 
    xmlns:ns2="http://schemas.xmlsoap.org/soap/http" 
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
    xmlns:tns="http://localhost:8080/myservicemembers/ns" 
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <wsdl:import location="http://localhost:8080/myservicemembers?wsdl=**MyService**.wsdl" 
        namespace="**http://service.me.com/**" /> 
        <wsdl:binding name="MyServiceImplServiceSoapBinding" type="ns1:**MyService**">
<!-- ... --> 
</wsdl:definitions> 

在应用程序上下文中使用"jaxws:endpoint"元素来更改端点的设置非常简单。这将充实服务名称、端点名称和其他字段。但是,顶级接口在WSDL文件中仍然有发言权。上面的STARRED项来自顶级界面如何将值注入targetNamespace和serviceName的顶级接口

我这样做的充分理由包括:(1)不想在WSDL中公开包名称;(2)希望在应用程序沿着部署跑道移动时切换名称空间。因此,我不能使用注释,因为它们是编译时的值,我不能用属性占位符替换它们,并且我不会在生产层重新编译我的代码。

您可以通过使用JaxWsServerFactoryBean而不是在Spring配置中使用<jaxws:endpoint>以编程方式创建服务来实现这一点。以编程方式进行创建可以为您提供更多的控制。

例如:

@Autowired
var myServiceImpl: MyService = _
val propMap = mutable.HashMap[String, AnyRef]("org.apache.cxf.logging.FaultListener"->faultListener.asInstanceOf[AnyRef])
val sf = new JaxWsServerFactoryBean
sf.setServiceBean(myServiceImpl)
sf.setAddress("/myservice")
sf.setServiceName(new QName(WEB_SERVICE_NAMESPACE, "myService", "MyService"))
sf.setProperties(propMap)
sf.create

最新更新