WebService CXF Spring

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebService CXF Spring相关的知识,希望对你有一定的参考价值。

 

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>CXF_Server</display-name>
  <!-- 添加  CXF 的Servlet ,处理 webservice的请求 -->
  <servlet>
  <servlet-name>cxf</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>cxf</servlet-name>
   <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
  <!-- Spring 监听添加 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
</web-app>

 

beans.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:p="http://www.springframework.org/schema/p"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
       <bean id="employeeManagerImpl" class="cn.it.ws.cxf.b.EmployeeManagerImpl"></bean>
  <!-- 配置cxf   
     地址:      http://192.168.114.10:8080/CXF_Server/ws/employeeManager
     组成 :  http://192.168.114.10:8080 +CXF_Server( 项目名)+ws(过滤的路径)+/employeeManager(自定义部分)
     服务类 :
     服务的实现类:
     拦截器  
      -->
  <jaxws:server address="/employeeManager" serviceClass="cn.it.ws.cxf.b.EmployeeManager">
   <jaxws:serviceBean>
    <ref bean="employeeManagerImpl"/>
   </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>

 

public class Employee {
    private Integer  id;
    private String name;
    private Integer age;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

}
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import cn.it.ws.cxf.bean.Employee;
@WebService(serviceName="EmployeeService")
public interface EmployeeManager {

    public abstract void add(@WebParam(name="employee")Employee employee);

    public abstract @WebResult(name="employees")List<Employee> query();

}

 

import java.util.ArrayList;
import java.util.List;

import cn.it.ws.cxf.bean.Employee;

/**员工管理的业务实现类*/
public class EmployeeManagerImpl implements EmployeeManager {
    private List<Employee> employees=new ArrayList<>();
    @Override
    public void add(Employee employee){
        //添加到集合中
        employees.add(employee);
    }
    @Override
    public List<Employee> query(){
        return employees;
    }

}

 

以上是关于WebService CXF Spring的主要内容,如果未能解决你的问题,请参考以下文章

Spring整合CXF之发布WebService服务

WebService -- Java 实现之 CXF ( 使用:Spring+CXF+Tomcat发布webService)

spring+cxf 开发webService(主要是记录遇到spring bean注入不进来的解决方法)

如何用java调用cxf webservice接口

Spring+CXF+Maven发布Webservice

学习webservice之cxf:Spring整合CXF