java利用CXF实现webservice

一直有想写博客的念头,只是人比较懒,一直没空来执行,最近看了下webservice的使用,多多少少有点收获,我想通过博客来写出自己学到的东西应该是印象深刻些,因此借此来实现写博客的念头,同时也锻炼下自己对现有知识表述能力,记录自己技术成长的道路历程。。。

       写在前面:知识纯属在书上或借鉴别人的博客所学再加上点个人理解,也可能都是借鉴他人的观点根本没有个人的理解。

       来来来。。。开始了。。

      

      谈到webservice大家可能对CXF和axis框架比较熟悉,这两个框架都是从已有的开源项目发展起来的。Axis2是从Axis1.x系列发展而来。CXF则是XFire和Celtix项目的结合产品。Axis2是从底层全部重新实现,使用了新的扩展性更好模块架构。 CXF也重新的深化了XFire和Celtix这两个开发工具,由于公司的开发环境已经集成了Axis2,有现成的调用例子,因此我就对CXF的怎么使用找资料简单粗略的学习了下。

       ​利用CXF发布webservice服务 别人可以调用 自己也可以调用

1、CXF中集成了spring 
因此我们可以利用代码来发布服务  也可以通过spring来管理启动发布服务
创建web项目:JSTLTest(项目自己起名)
导入cxf的包:网上有下载。
 
下面介绍通过spring管理来发布webservice
写Spring的配置文件: (我是直接放在src根目录下的,后面的web.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd">
    
    <!-- 
        通过Spring 管理CXF 
          1: 创建业务逻辑Bean(如果要发布WebService则需要添加相应注解) 并且配置到Spring 配置文件中
          2: 通过jaxws标签 发布WS服务, 指定路径, 和输入 输出日志
          3: 配置CXFServlet 指定一个专门用来访问WS的路径
          4: 通过Spring的监听器,加载Spring配置文件       
     -->
    
    <!-- 配置业务逻辑bean -->     
    <bean class="com.webservice.service.StudentServiceImpl" id="studentService" />
    <!-- 配置基于JAX WS的WS服务Bean 
        address: 只需要取一个名称即可,完整的路径: http://localhost:8080/ws/myWs/demo
         http://localhost:8080//JSTLTest//ws//whoWs?wsdl  
        serviceBean="" :不用在指定, 采用引用的方式
    -->
    <jaxws:server address="/whoWs">
        <jaxws:serviceBean>
            <!-- 参照指定的业务逻辑Bean, 这样配置此Bean只创建一次,而且本地和远程都可以访问 -->
            <ref bean="studentService"/>
        </jaxws:serviceBean>
        <!-- 配置输入和输出日志 -->
        <jaxws:inInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
        </jaxws:inInterceptors>
        <jaxws:outInterceptors>
            <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
        </jaxws:outInterceptors>
    </jaxws:server>
    
</beans> 
 
2、因为没有数据和其他显示层的框架,可以用servlet来处理页面请求
现在来写个service发布服务,编写的不同类,要在上面加注解 @WebService才会被当做是服务,并且还要在
web.xml中配置
这里我写的类: StudentServiceImpl .java
package com.webservice.service;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import com.webservice.domain.Student;
@WebService
public class StudentServiceImpl {
    private List<Student> studentList = new ArrayList<Student>();
    
    public StudentServiceImpl() {
        super();
        String address = "http://localhost:8080//JSTLTest//ws//whoWs";
        System.out.println("wsdl服务地址:" + address + "?wsdl");
    }
    // 接收的是一个对象
    public void save(Student student) {
        System.out.println("----save----");
        studentList.add(student);
    }
    // 查询所有的学生信息
    public List<Student> query() {
        System.out.println("----query----");
        return studentList;
    }
    // 查询指定学生信息
    public Student get(@WebParam(name="id"int id) {
        System.out.println("----get----");
        for (Student temp : studentList) {
            if (temp.getId() == id) {
                return temp;
            }
        }
        return null;
    }
}
 
3、接下写web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JSTLTest</display-name>
  <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>/ws/*</url-pattern><!--与上面的路径对应起来-->
  </servlet-mapping>
<!--
这是处理请求的servlet,这里我没有给我  因为我没有写前台请求的界面来请求
只是做了个简单的测试
 
 <servlet>
    <servlet-name>WebserviceSer</servlet-name>
    <servlet-class>com.webservice.WebserviceSer</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>WebserviceSer</servlet-name>
    <url-pattern>/servlet/WebserviceSer</url-pattern>
  </servlet-mapping>
-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>  
   com.lisener.TaskLisener 
  </listener-class>
  </listener>
  
</web-app>  
 
4、启动tomcat,这里只测试了tomcat,其他的web容器可以自己试下
通过请求 http://localhost:8080//JSTLTest//ws//whoWs?wsdl
来获取wsdl文件内容,内容是从下往上看,服务名和服务中的方法以及方法中参数类型、返回值都会在这个文件中有 映射关系。
通过这个wsdl文件内容可以通过CMD命令:wsimport -p com.sinobest.wsservice -s . http://localhost:8080//JSTLTest//ws//whoWs?wsdl 获取到发布的服务代码
 
5、第三方怎么来请求这个服务呢?
就是将上面cmd命令生成的代码,放在自己的项目中,使用即可
这里我新建了 Ws_client项目,将cmd命令生成的代码放到了这个项目中
然后写了个简单的例子: WSClientTest  .java
package com.sinobest.wsclient;
 
import com.sinobest.wsservice.Student;
import com.sinobest.wsservice.StudentServiceImpl;
import com.sinobest.wsservice.StudentServiceImplService;
 
public class WSClientTest {
public static void main(String[] args) {
StudentServiceImplService ws = new StudentServiceImplService();
StudentServiceImpl soap = ws.getStudentServiceImplPort();
 
Student student = new Student();
student.setId(1);
student.setName("aaa");
student.setAge(34);
student.setAddress("天朝帝国");
 
 
soap.save(student);
 
System.out.println(soap.get(1).getName());
}
 
 
}
 
好了,我已经测试通过了。。。
 
 
 
 
 
 
 
 
 
 

      

猜你喜欢

转载自haiou33.iteye.com/blog/2198425
今日推荐