`

cxf在web中与spring集成

阅读更多

cxf version:2.7

jre:1.7

 

建立web工程cxfweb,将cxf的lib目录下面的所有jar包进入到web工程

 

web.xml:

  <context-param>

  <param-name>contextConfigLocation</param-name>

       <!--         cxf的配置文件位置            -->

  <param-value>classpath:cxf-servlet.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>

</servlet>

 

<servlet-mapping>

<servlet-name>cxf</servlet-name>

<url-pattern>/cxf/*</url-pattern>

</servlet-mapping>

 

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"

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-3.0.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">

<!-- 引入以下文件,以保证不会出错,文件在jar包里-->

<import resource="classpath:META-INF/cxf/cxf.xml" />

<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

 

<!-- id:唯一标示 serviceClass:接口类型 address:服务的请求url-->

<jaxws:server id="helloService" serviceClass="com.wdh.ws.cxf.IHelloService" address="/hello">

<jaxws:serviceBean>

<!-- 服务的实现类 -->

<bean class="com.wdh.ws.cxf.HelloServiceImpl"></bean>

</jaxws:serviceBean>

<!-- 加入请求的消息拦截器,不加也可以 -->

<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:server>

 

</beans>

 

java 代码:

package com.wdh.ws.cxf;

 

import javax.jws.WebService;

import javax.xml.ws.BindingType;

import javax.xml.ws.soap.SOAPBinding;

 

@WebService

@BindingType(SOAPBinding.SOAP12HTTP_BINDING)//sopa 1.2

public interface IHelloService {

 

String sayHello(String name);

}

 

package com.wdh.ws.cxf;

 

public class HelloServiceImpl implements IHelloService {

 

@Override

public String sayHello(String name) {

return "hello " + name;

}

 

}

 

如果我的wen工程路径为:

    http://192.168.1.116:9999/cxfweb

那么该webservice的地址为:

  http://192.168.1.116:9999/cxfweb/cxf/hello?wsdl

 

客户端生成代理对象:

   启动命令行,切换到cxf的bin目录,运行

   D:\apache-cxf-2.7.11\bin>wsdl2java -d e: http://192.168.1.116:9999/cxfweb/cxf/hello?wsdl

 

客户端测试代码:

package com.wdh.ws.cxf;

 

public class CxfTest {

 

public static void main(String[] args) {

IHelloServiceService helloServiceService = new IHelloServiceService();

IHelloService helloService = helloServiceService.getIHelloServicePort();

String str = helloService.sayHello("jack");

System.out.println(str);

}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics