基于spring4.3.6,cxf3.0.12的WebService开发

基于spring4.3.6,cxf3.0.12的WebService开发

由于项目使用Spring开发,所以笔者选择了Apache CXF进行WebService开发,原因是Apache CXF 提供方便的Spring整合方法,
可以通过注解、Spring标签式配置来暴露Web Services和消费Web Services。
1.    首先去http://cxf.apache.org/download.html 下载最新的版本(目前是3.0.12)
2.    Maven pom依赖相应的包到项目中,大概包如下:
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.0.12</version>
</dependency>
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.0.12</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.6.RELEASE</version>
</dependency>
3.    新建一个接口

       @WebService

public interface HelloWorldService {

              public String getNewName(String userName);

}
使用@WebService标识让CXF知道使用该接口来创建WSDL
4.    新建一个实现类

       @WebService(endpointInterface="org.app.demo.spring.service.HelloWorldService")

public class HelloWorldServiceImpl implements HelloWorldService {

              public String getNewName(String userName) {

                     return "Hello Spring!" + userName;

              }

}
5.    修改相应的配置文件
ApplicationContext.xml中加入如下代码

<jaxws:endpoint

         id="helloWorld"

         implementor="org.app.demo.spring.service.impl.HelloWorldServiceImpl"

           address="/HelloWorld" />
或者
<bean id=" helloWorldService" class=" org.app.demo.spring.service.impl.HelloWorldServiceImpl" />

<jaxws:endpoint

         id="helloWorld"

         implementor="#helloWorldService"

           address="/HelloWorld" />
 
注意:XML头文件需相应添加
xmlns:jaxws="http://cxf.apache.org/jaxws
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
 
web.xml中加入如下代码

<servlet>

              <servlet-name>CXFServlet</servlet-name>

              <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>

              <load-on-startup>2</load-on-startup>

       </servlet>

 

       <servlet-mapping>

              <servlet-name>CXFServlet</servlet-name>

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

</servlet-mapping>
6.    发布项目到tomcat 服务器。
7.    启动Tomcat,打开http://localhost:8080/myapp/HelloWorld?wsdl就可以看到了
8.    客户端调(java project)用也很方便,如下代码:

       URL wsdlUrl = new URL("http://192.168.0.114:8080/myapp/HelloWorld?wsdl");

       QName SERVICE_NAME = new QName("http://impl.service.spring.demo.app.org/", "HelloWorldServiceImplService");

       Service service = Service.create(wsdlUrl, SERVICE_NAME);

       HelloWorldService hello = service.getPort(HelloWorldService.class);

System.out.println(hello.getNewName("WebService调用"));
8.客户端调(java web) ApplicationContext.xml中加入如下代码

<bean id="helloWorld"

class="org.app.demo.spring.service.HelloWorldService"

      factory-bean="clientFactory" factory-method="create"/>

   

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">

      <property name="serviceClass" value=" org.app.demo.spring.service.HelloWorldService"/>

      <property name="address" value=" http://192.168.0.114:8080/myapp/HelloWorld "/>

     </bean>

类 @Autowired

     private HelloWorldService helloWorld;
然后执行代码后,会打印出 Hello Spring!WebService调用。
注意   1. IP地址为Webservice的服务器的地址。
         2. 必须把相应的接口复制到客户端项目中。

转载于:https://my.oschina.net/u/3213541/blog/850037

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值