`

简单webservice(cxf)实例

阅读更多
我们的环境是eclipse , maven。

在开发java webservice时,有两个比较流行的框架:axis2和cxf。cxf可以无缝的和spring集成,而axis2需要打包成aar文件,在tomcat下面跑。于是我们选择了cxf开发webservice。

下面一步步的介绍用cxf开发服务端和客户端的例子。

首先开发服务端:

一:在pom.xml文件中加入依赖:

  <dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-api</artifactId>
   <version>2.5.0</version>
  </dependency>
  <dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-frontend-jaxws</artifactId>
   <version>2.5.0</version>
  </dependency>
  <dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-bindings-soap</artifactId>
   <version>2.5.0</version>
  </dependency>
  <dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-transports-http</artifactId>
   <version>2.5.0</version>
  </dependency>
  <dependency>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-rt-ws-security</artifactId>
   <version>2.5.0</version>
  </dependency>

然后会在maven dependencies文件夹中看到引用的jar包

二:开发服务的接口类和实现类

首先接口类:

package com.mycompany.webservice.server;

import javax.jws.WebService;

@WebService
public interface Greeting {
   public String greeting(String userName);
}

然后实现类:

package com.mycompany.webservice.server;

import java.util.Calendar;
import javax.jws.WebService;

@WebService(endpointInterface = "com.mycompany.webservice.server.Greeting")
public class GreetingServiceImpl implements Greeting {

public String greeting(String userName) {
  return "Hello " + userName + ", currentTime is "
    + Calendar.getInstance().getTime();
}
}
注意上面实现类的endpointInterface的接口类路径不要写错,否则会报Could not load Webservice SEI

服务端的代码开发就到此了。下面要配置文件。

在application.xml中:

添加
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/cache/spring-context-3.2.xsd
        http://cxf.apache.org/jaxws  
   http://cxf.apache.org/schemas/jaxws.xsd"
       default-autowire="byName" default-lazy-init="true">
<!--实现类的bean,需要spring注入 -->

<bean id="greetingServiceImpl" class="com.mycompany.webservice.server.GreetingServiceImpl"/>
<jaxws:endpoint id="greetingService"  implementor="#greetingServiceImpl"   address="/Greeting" />

在web.xml中:

添加

<servlet> 
        <servlet-name>CXFServlet</servlet-name> 
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>CXFServlet</servlet-name> 

<!--==这个设置很重要,那么我们的webservice的地址就是http://localhost:8080/yourProgramName/webservice/Greeting===-->
        <url-pattern>/webservice/*</url-pattern> 
    </servlet-mapping>

好了。到此我们的服务端就开发好了。。

就可以通过http://localhost:8080/yourProgramName/webservice/Greeting?wsdl 访问看到我们的wsdl了。

下面开发我们的客户端,来调用上面的服务。

package com.mycompany.webservice.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;


public class GreetingServiceClient {
public static void main(String[] args) {
  //创建WebService客户端代理工厂
  JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  //注册WebService接口
  factory.setServiceClass(Greeting.class);
  //设置WebService地址
  factory.setAddress("http://localhost:8080/yourProgramName/webservice/Greeting");
  Greeting greetingService = (Greeting)factory.create();
  System.out.println("invoke webservice...");
  System.out.println("message context is:"+greetingService.greeting("gary"));  
}
}

好了客户端也开发完成。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics