如何设置其WS-Discovery插件使用的CXF服务的publishedUrl?

时间:2014-03-15 22:19:08

标签: spring servlets cxf ws-discovery

我正在尝试使用CXF WS-Discovery插件发布CXF服务的URL。

教程https://cxf.apache.org/docs/writing-a-service-with-spring.html帮助我构建了一个helloworld服务。 WS-Discovery https://cxf.apache.org/docs/ws-discovery.html的CXF文档解释了添加cxf-services-ws-discovery-service.jar允许发布它。

很好,但发布的url是相对于servlet的,然后无法从发送WS-Discovery探测器的客户端访问。

我找到了一个有趣的方法http://osdir.com/ml/users-cxf-apache/2012-05/msg00524.html,建议使用以下web.xml和cxf-servlet.xml文件:

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app.xsd">
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/webservices/*</url-pattern>
    </servlet-mapping>  
</web-app>

的cxf-servlet.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <bean id="localhost" class="java.net.InetAddress" factory-method="getLocalHost" />
    <bean id="publishedWebServiceUrl" class="java.lang.String">
        <constructor-arg value="#{'http://' + localhost.hostAddress + ':8080' + servletContext.contextPath + '/webservices/hello_world'}"/>
    </bean>

    <jaxws:endpoint id="hello_world" implementor="HelloWorldImpl"  address="/hello_world">
                <jaxws:properties>
                        <entry key="publishedEndpointUrl" ><ref bean="publishedWebServiceUrl" /></entry>
                </jaxws:properties>
    </jaxws:endpoint>
</beans>

如果servlet容器使用端口8080,这可以正常工作。

我尝试使用servletContext.getRealPath('/webservices'),但这会提供文件系统路径,而不是http地址。

有没有办法获得servlet容器端口(符合tomcat,jetty,...)?或其他方式发布可导出的URL?

1 个答案:

答案 0 :(得分:0)

我担心你需要从外部配置地址,我不认为CXF可以告诉端口servlet容器正在使用。