您的位置:首页 > 编程语言 > Java开发

webservice:cxf整合spring入门

2017-07-22 15:24 375 查看
一,服务端

创建maven工程,打成war

在main/webapp下加入WEb-INF/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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<servlet>
<servlet-name>webservice</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>webservice</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
</web-app>
3.在pom.xml中加入spring/cxf的依赖(注意jdk要用1.7的,1.5不支持webservice的一些功能)

<properties>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.1</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>


4.编写webService接口,在接口上加上注解@WebService

5.编写实现类

6.在src/main/resources里添加applicationContext_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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <bean id="myWeather" class="com.lab.myweather.impl.MyWeatherImpl"></bean>

<jaxws:server address="/weather">
<jaxws:serviceBean>
<ref bean="myWeather"></ref>
</jaxws:serviceBean>
</jaxws:server>

</beans>


7.综合web.xml,application_cxf.xml,访问路径为http://localhost/MyCXFService/ws/weather?wsdl

8.在浏览器上访问,wsdl:service name=”MyWeatherImplService”>知MyWeatherImpl为类名,wsdl:operation name=”getWeather”> 知getWeather为方法

wsdl:import location=”http://localhost/MyCXFService/ws/weather?wsdl=MyWeather.wsdl” namespace=”http://myweather.lab.com/”>

访问上面的路径http://localhost/MyCXFService/ws/weather?wsdl=MyWeather.wsdl

<xs:complexType name="getWeather">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getWeatherResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
可以知道参数和返回值,带response的为返回值,不带的为参数,上例参数为String,返回值为String


客户端

创建maven工程,打成jar包

在src/main/java文件夹,shift+右键,在此处打开命令窗口

输入wsimport -s . http://localhost/MyCXFService/ws/weather?wsdl

回车

回到eclipse刷新src/main/java文件夹,可以看到生成了很多文件

添加一个与服务端一样的pom.xml

添加一个application_cxf.xml 注意下面serviceClass为生成的接口全路径

编写测试类测试就好

<?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.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> 
<jaxws:client id = "weather" serviceClass="com.lab.myweather.impl.MyWeather" address="http://localhost/MyCXFService/ws/weather?wsdl">

</jaxws:client>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: