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

webservice学习之cxf框架-spring整合

2016-10-20 13:26 357 查看

1.思路:

Cxf框架本身依赖spring,从官方下载cxf包中有spring的jar包。

 

上边使用JaxWsServerFactoryBean和JaxWsProxyFactoryBean,改为spring配置方式。

 

发布服务:使用spring和cxf整合的标签<jaxws:server

 

客户端调用服务:使用spring和cxf整合的标签<jaxws:client

 

上边的<jaxws:server和<jaxws:client相当于spring容器的bean,发布服务和客户端调用交给spring容器管理。

2.服务端:

2.1创建web工程

2.2假如cxf的jar包

2.3开发SEI和实现类:

Cxf开发SEI需要将@webservice注解加在接口中.

SEI接口:

<pre name="code" class="java">@WebService(
targetNamespace="http://weather.itcast.cn/",//指定 wsdl的命名空间
name="WeatherInterface",//指定portType的名称
portName="WeatherInterfacePort",//指定port的名称
serviceName="WeatherService"//服务视图的名称
//endpointInterface="cn.itcast.ws.service.WeatherInterface2"//指定哪个接口中方法要发布成webservice服务,接口中加上@webservice注解
)
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {

//查询三天天气
public @WebResult(name="result") List<WeatherModel> queryWeather(@WebParam(name="cityName") String cityName);

}



SEI实现类:

public class WeatherInterfaceImpl implements WeatherInterface {

@Override
public  List<WeatherModel> queryWeather(String cityName) {

//构造三天天气
List<WeatherModel> list = new ArrayList<WeatherModel>();

Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DATE);

WeatherModel weatherModel_1  =new WeatherModel();
weatherModel_1.setDetail("晴");
weatherModel_1.setData(new Date());
weatherModel_1.setTemperature_max(5);
weatherModel_1.setTemperature_min(-6);

WeatherModel weatherModel_2  =new WeatherModel();
weatherModel_2.setDetail("阴");
calendar.set(Calendar.DATE, day+1);
weatherModel_2.setData(calendar.getTime());
weatherModel_2.setTemperature_max(10);
weatherModel_2.setTemperature_min(-3);

WeatherModel weatherModel_3  =new WeatherModel();
weatherModel_3.setDetail("晴");
calendar.set(Calendar.DATE, day+2);
weatherModel_3.setData(calendar.getTime());
weatherModel_3.setTemperature_max(2);
weatherModel_3.setTemperature_min(-9);

list.add(weatherModel_1);
list.add(weatherModel_2);
list.add(weatherModel_3);

return list;
}

}


2.4编写applicationContext.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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> 
<!-- service -->
<bean id="weatherInterface" class="cn.itcast.ws.service.WeatherInterfaceImpl"/>

<!-- 发布服务
使用jaxws:server和jaxws:endpoint可以发布服务
webservice地址=tomcat地址+cxf servlet的路径+/weather
-->
<jaxws:server address="/weather" serviceClass="cn.itcast.ws.service.WeatherInterface">
<jaxws:serviceBean>
<ref bean="weatherInterface"/>
</jaxws:serviceBean>
</jaxws:server>

</beans>


2.5 在web.xml中配置cxf的servelt ,cxf的servlet用于解析cxf发布webservice

<!-- cxf的servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 本系统webservice的路径必须以/ws/开头 -->
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>


2.6 在web.xml中加载spring

<!-- 加载 spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


2.7 启动tomcat测试,输入:http://localhost:8080/ws_1231_cxf_spring_server/ws

2.8 测试注意:

因为通过spring和cxf整合将webservice通过tomcat发布,webservice和应用程序共用一个端口都是8080。

测试webservice和应用程序(jsp)是否可以共存(都可以访问)。

正式上线使用80端口。

3.客户端

3.1使用<jaxws:client调用服务端,<jaxws:client需要cxf和spring整合环境。

3.2使用wsdl2java/wsimport工具生成客户端调用代码
3.3编写applicationContext.xml

<span style="font-size:12px;font-weight: normal;"><?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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"> 
<!-- 使用<jaxws:client调用服务端
jaxws:client内部使用JaxWsProxyFactoryBean方式
serviceClass:指定portType地址(需要使用wsdl2java工具生成)
-->
<jaxws:client id="weatherClient" address="http://localhost:8080/ws_1231_cxf_spring_server/ws/weather?wsdl"

serviceClass="cn.itcast.weather.WeatherInterface">

</jaxws:client>

</beans></span>

3.4测试
public class ClientTest {

private ApplicationContext applicationContext;

@Before
public void before() {

applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");

}

@Test
public void testCxfSpringClient() {
// 从spring容器中取出porttype
WeatherInterface weatherInterface = (WeatherInterface) applicationContext
.getBean("weatherClient");

// 调用portType的方法
List<WeatherModel> list = weatherInterface.queryWeather("郑州");

for (WeatherModel weatherModel : list) {
System.out.println(weatherModel.getDetail());
Date date = weatherModel.getData().toGregorianCalendar().getTime();
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(date));
System.out.println(weatherModel.getTemperatureMax());
System.out.println(weatherModel.getTemperatureMin());
}
}

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