简单地基于cxf框架的webservice完整开发示例

这里用到的是Apache的CXF框架,使用的版本为2.7.1,可以去官网下载相应的jar包,这里使用了maven来构建项目,pom文件中添加用到的依赖
	        <dependency>
                 <groupId>org.apache.cxf</groupId>
                 <artifactId>cxf-rt-frontend-jaxws</artifactId>
                 <version>2.7.1</version>
            </dependency>
            <dependency>
                 <groupId>org.apache.cxf</groupId>
                 <artifactId>cxf-rt-transports-http</artifactId>
                 <version>2.7.1</version>
            </dependency>
            <dependency>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-web</artifactId>
                 <version>3.1.2.RELEASE</version>
            </dependency>
下面直接进入正题:
 
1.编写service接口
package com.cj.webservice;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
    String sayHi(String text);
}

注:这里只是作为简单的sample开发示例展示,暴露的接口较为简单。

 

2.编写service接口实现类
package com.cj.webservice;
import javax.jws.WebService;
@WebService(endpointInterface = "demo.hw.server.HelloWorld",
            serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    @Override
    public String sayHi(String text) {
        System.out.println("sayHi called");
        return "Hello" + text ;
    }
}
说明:
1.@WebService解在实现类上,目的是创建WSDL的时候让CXF知道哪个接口被使用。这里就是HelloWorld接口。
2.endpointInterface:接口类 ,serviceName:服务名
 
3.将服务注册到web容器中
 
这里注册方式有两种,第一是通过定义cxf-servlet.xml来注册,第二是将服务注册到其他的xml中,通过加载其他的xml来注册服务,下面依次来说明两种方式的使用:
 
①.通过定义cxf-servlet.xml来加载:
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:soap="http://cxf.apache.org/bindings/soap"
      xsi:schemaLocation= "
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">

     <jaxws:server id="jaxwsService" serviceClass="demo.hw.server.HelloWorld"
            address="/hello_world">
            <jaxws:serviceBean>
                 <bean class="demo.hw.server.HelloWorldImpl" />
            </jaxws:serviceBean>
     </jaxws:server>
     <!-- 也可以通过下面定义的方式注册 -->
     <!-- <jaxws:endpoint id="jaxwsService" address="/hello_world" implementor="demo.hw.server.HelloWorldImpl">
           </jaxws:endpoint> -->
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version= "2.5" xmlns ="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
     <display-name>cxf</display-name>
     <servlet>
            <servlet-name>cxf</servlet-name>
           <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
     </servlet>
     <servlet-mapping>
            <servlet-name>cxf</servlet-name>
            <url-pattern>/services/*</url-pattern>
     </servlet-mapping>
</web-app>
说明:
1.定义的cxf servlet通过读取默认的WEB-INF/cxf-servlet.xml来加载,进行请求拦截。
2.注册服务,可以通过<jaxws:server>标签,也可以通过<jaxws:endpoint>标签。一般指定serviceClass(接口),serviceBean(实现类),address(访问地址)这三个属性即可。
 
②.通过其他的xml来注册
service.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:soap= "http://cxf.apache.org/bindings/soap"
      xsi:schemaLocation= "
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd" >

     <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" />
     
     <jaxws:server id ="jaxwsService" serviceClass="demo.hw.server.HelloWorld"
            address= "/hello_world">
            <jaxws:serviceBean >
                 <bean class ="demo.hw.server.HelloWorldImpl" />
            </jaxws:serviceBean>
     </jaxws:server >
     <!-- <jaxws:endpoint id="jaxwsService" address="/hello_world" implementor="demo.hw.server.HelloWorldImpl">
           </jaxws:endpoint> -->
</beans>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
     <display-name>cxf</display-name>
     <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/service.xml</param-value>
     </context-param>
     <listener>
           <listener-class >org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     <servlet>
            <servlet-name>cxf</servlet-name>
           <servlet-class>org.apache.cxf.transport.servlet.CXFServlet </servlet-class>
            <load-on-startup>1 </load-on-startup>
     </servlet>
     <servlet-mapping>
            <servlet-name>cxf</servlet-name>
            <url-pattern>/services/*</url-pattern>
     </servlet-mapping>
</web-app>
说明:
1.这里将服务注册到其他xml中,并通过启动容器加载其他xml来注册。
2.CXF用到的cxf-servlet.xml通过import的方式来加载。
 
上述两种方式都可以启用web service,实际场景中一般都选用第二种方式来实现。
 
4.验证web service是否发布成功
 
这里使用的web容器为Tomcat,将项目部署到容器中去,启动后,输入:
http://localhost:8080/java_first_jaxws/services 即可查看到可用的service,通过输入
http:// localhost:8080/java_first_jaxws/services/hello_world?WSDL 便可看到发布的HelloWorld生成的wsdl文件,WSDL即Web Services Description Language,服务描述语言,是一个用来描述Web服务和说明如何与Web服务通信的XML语言。具体的webservice相关术语,感兴趣的可自行google。
 
5.客户端使用web service
 
客户端创建一个Service,通过设置服务的相关属性即可:
package demo.hw.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import demo.hw.server.HelloWorld;
import demo.hw.server.User;
import demo.hw.server.UserImpl;

public final class Client {

    public static void main(String args[]) throws Exception {
        String endpointAddress = "http://localhost:8080/java_first_jaxws/services/hello_world";
        JaxWsProxyFactoryBean clientFactory = new JaxWsProxyFactoryBean();
        clientFactory.setAddress(endpointAddress);
        clientFactory.setServiceClass(HelloWorld.class);
       
        HelloWorld hw = (HelloWorld)clientFactory.create();
        System.out.println(hw.sayHi("World"));
    }
}
运行以上代码,可以看到:
服务端:sayHi called
客户端:Hello World
 
当然,实际场景中客户端一般也是通过bean的方式注册到Spring配置文件中去。通过Spring来创建Client,也有两种创建方式,一种通过<jaxws:client>标签,还有一种就是类似上面的工厂方式,用JaxWsProxyFactoryBean来创建,具体使用示例分别如下:
<jaxws:client>标签:
<jaxws:client id= "client" serviceClass="demo.hw.server.HelloWorld"
         address="http://127.0.0.1:8080/java_first_jaxws/services/hello_world" />
JaxWsProxyFactoryBean工厂:
<bean id="client" class="demo.hw.server.HelloWorld" factory-bean="clientFactory" factory-method="create" />
<bean id ="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
      <property name ="serviceClass" value= "demo.hw.server.HelloWorld" />
      <property name ="address" value= "http://127.0.0.1:8080/java_first_jaxws/services/hello_world" />
</bean>
客户端拿到该"client" bean即可:
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:conf/spring/cxf/cxf-client.xml" );
HelloWorld hw = (HelloWorld)ac.getBean("client");
System.out.println(hw.sayHi("chenjie"));
 
至此,简单的基于cxf框架的webservice完整开发示例已经over,相信就算不懂webservice的人,跟着以上步骤敲一遍,也能运行出结果来。这只是作为一个简单的开发示例,实际应用场景上要复杂些,但作为入门示例已经足够了。

 









 


 

你可能感兴趣的:(webservice,CXF,web服务)