您的位置:首页 > 运维架构 > Tomcat

CXF系列之JAX-WS:与Spring3集成并在tomcat部署

2016-11-25 15:39 495 查看
虽然网上的资料很多,但自己还是有必要记录一下,毕竟自己整理的,查询容易,对以后也方便

该集成是基于CXF3.0的。

web service 接口和实现类,都很简单,仅仅是为了测试环境,是否搭建成功。

package com.test.service.inter;

import javax.jws.WebService;

@WebService
public interface HelloService {

public String sayHello(String name);
}

package com.test.service.impl;

import com.test.service.inter.HelloService;

public class HelloServiceImpl implements HelloService {

@Override
public String sayHello(String name) {
return "您好:"+name;
}

}
spring配置文件:spring.xml,主要有两个功能:1、spring相关配置2、引入spring与cxf集成文件

第一种方式:

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
<import resource="spring-cxf.xml" />

</beans>
spring与cxf集成文件:spring-cxf.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-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 
<jaxws:endpoint id="helloService" implementor="com.test.service.impl.HelloServiceImpl" address="/hello"/>

</beans>


第二种方式:是由Spring装配bean

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
<bean id="helloService" class="com.test.service.impl.HelloServiceImpl"></bean>

<import resource="spring-cxf.xml" />

</beans>
<?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-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 
<jaxws:endpoint implementor="#helloService" address="/hello"/>

</beans>
注意:<jaxws:endpoint>的属性implementor引用的是bean的id,但必须以“#”开头,这是cxf特有的写法。

同样,在<jaxws:server serviceClass="#helloService">也是可以的。

在配置集成文件,应该注意以下几点:
1、引入jaxws命名空间

2、两种部署方式:

(1)、<jaxws:endpoint ...>方式,相当于:

Endpoint.publish("http://localhost:8080/helloService", new HelloServiceImpl());

属性implementor表示需要部署的web服务实现类,address表示访问地址,一个标示,表示访问的是具体的哪个Web服务。

(2)、

<jaxws:server  serviceClass="com.test.service.impl.HelloServiceImpl" address="/hello" ></jaxws:server>
相当于:JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();

其中serviceClass属性用来设置Web服务实现类,address用来设置访问地址,与(1)一样。

(3)、配置拦截器

<jaxws:endpoint id="helloService" implementor="#helloServiceImpl" address="/soap/hello">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>

web配置文件: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_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springCxfWebservice</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- CXF -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
这个没什么好说的,(1)配置Spring监听器以及spring配置文件,(2)配置CXFServlet。

将以上项目部署到tomcat中,启动tomcat,并访问:
http://localhost:8080/springCxfWebservice(项目名称或者你设置的对外部署名称)/services,显示如下界面:


显示WSDL文件:http://localhost:8080/springCxfWebservice/services/hello?wsdl,如下:

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.test.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://inter.service.test.com/" name="HelloServiceImplService" targetNamespace="http://impl.service.test.com/">
<wsdl:import location="http://localhost:8080/springCxfWebservice/services/hello?wsdl=HelloService.wsdl" namespace="http://inter.service.test.com/"></wsdl:import>
<wsdl:binding name="HelloServiceImplServiceSoapBinding" type="ns1:HelloService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloServiceImplService">
<wsdl:port binding="tns:HelloServiceImplServiceSoapBinding" name="HelloServiceImplPort">
<soap:address location="http://localhost:8080/springCxfWebservice/services/hello"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
客户端:

配置文件:spring-client.xml,不能够放在WEB-INF下

<?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-4.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 
<jaxws:client id="helloService"
serviceClass="com.test.service.inter.HelloService"
address="http://localhost:8080/springCxfWebservice/services/hello"/>

</beans>
客户端代码:

package com.test.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.service.inter.HelloService;

public class Client {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-client.xml");
HelloService helloService = context.getBean("helloService", HelloService.class);
String result = helloService.sayHello("cxf");
System.out.println(result);
}
}


执行结果:

您好:cxf
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cxf tomcat spring