使用CXF开发JAX-WS类型的WebService

使用CXF记得要先加入CXF的jar包

方法1:

Cxf编程实现:

  • l 使用jaxwsServerFactoryBean发布 webservice服务端。

需要设置:

  1. jaxwsServerFactoryBean.setAddress(“webservice地址”)
  2. jaxwsServerFactoryBean.setServiceClass(“porttype类路径”);//由程序员编写的
  3. jaxwsServerFactoryBean.setServiceBean(“porttype类对象”);
  4. jaxwsServerFactoryBean.create();//发布一个服务
public class WeatherServer {

    public static void main(String[] args) {

        //使用jaxWs发布soap协议的webservice
JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
//指定 webservice地址
jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/weather");
//指定 porttype
jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class); //指定服务类对象
jaxWsServerFactoryBean.setServiceBean(new WeatherInterfaceImpl()); //发布服务
jaxWsServerFactoryBean.create(); } }

l 使用jaxwsProxyFactoryBean实现客户端调用webservice服务

  1. jaxwsServerFactoryBean.setAddress(“webservice的wsdl地址”)
  2. jaxwsServerFactoryBean.setServiceClass(“porttype类路径”);//portType是由wsdl2java工具生成
  3. jaxwsServerFactoryBean.setCreate();//创建porttype对象
public class WeatherClient {

    public static void main(String[] args) {
// jaxWsProxyFactoryBean调用webservice服务端
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
// 调用地址
jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:12345/weather?wsdl");
// 设置portType
jaxWsProxyFactoryBean.setServiceClass(WeatherInterface.class);
// 创建portType
WeatherInterface weatherInterface = (WeatherInterface) jaxWsProxyFactoryBean
.create(); // 调用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());
} } }

方法2:

Cxf和spring整合开发服务端和客户端。

  • l 使用<jaxws:Server 发布 webservice服务端

  在<jaxws:Server设置  Address、serviceClass、serviceBean

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

接口:

@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)表示基于soap1.2协议,不写是1.1
@WebService(
targetNamespace="http://weather.allan.cn/",//指定 wsdl的命名空间
name="WeatherInterface",//指定portType的名称
portName="WeatherInterfacePort",//指定port的名称
serviceName="WeatherService"//服务视图的名称
//endpointInterface="cn.allan.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); }
  • l 使用<jaxws:Client 调用webservice服务

在<jaxws:Client设置  Address、serviceClass

<!-- 使用<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.allan.weather.WeatherInterface"> </jaxws:client>

测试客户端

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());
}
} }

web.xml配置

    <!-- 加载 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> <!-- 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>
上一篇:web相关


下一篇:Python之转换py文件为无需依赖python环境的exe文件的方法