java webService之CXF的使用

阅读更多
使用场景:华为VOD系统与媒资系统的接口,资产数据(元数据XML、正片、片花、海报等)采用FTP方式传输,系统间消息采用WebService方式进行通信。

框架:采用webService的CXF框架

具体实现操作:在已有的项目中集成CXF框架,提供webService服务接口;
1、依赖CXF所需的依赖包
  
            org.apache.cxf
            cxf-rt-frontend-jaxws
            2.7.13
        
        
            org.apache.cxf
            cxf-rt-transports-http
            2.7.13
        
        
            org.apache.cxf
            cxf-rt-transports-http-jetty
            2.7.13
        
        
            org.apache.cxf
            cxf-rt-ws-security
            2.7.13
        
        
            org.apache.cxf
            cxf-rt-ws-policy
            2.7.13
        
        
            org.apache.cxf
            cxf-bundle-jaxrs
            2.7.13
        
        
            javax.ws.rs
            jsr311-api
            1.1.1
        

2、开发接口样例:
UserInfo.java对象作为返回值
package com.jiahejiankang.operation.api.test;
public class UserInfo {
    private String name;
    private String phone;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

UserService.java接口
package com.jiahejiankang.operation.api.test;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(serviceName="userService",targetNamespace = "http://localhost/user/service")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface UserService {
    @WebMethod
    @WebResult(name = "userReturn", partName = "userReturn")
    UserInfo getUserInfo(@WebParam(name = "name", partName = "name")String name, String phone);
}

UserServiceImpl.java 接口实现类
package com.jiahejiankang.operation.api.test;
public class UserServiceImpl implements UserService {
    public UserInfo getUserInfo(String name, String phone) {
        UserInfo userInfo = new UserInfo();
        userInfo.setName(name);
        userInfo.setPhone(phone);
        System.out.println(name+":"+phone);
        return userInfo;
    }
}

测试类:
package com.jiahejiankang.operation.api.test;

import javax.xml.ws.Endpoint;
public class TestMain {
    public static void main(String[] args) {
        System.out.println("Server is starting...");
        UserService userService = new UserServiceImpl();
        Endpoint.publish("http://localhost:8080/user/service",userService);
        System.out.println("Server is started...");
    }
}


这里主要是测试接口是否可用;通过 http://localhost:8080/user/service??wsdl浏览器链接可以得到




















当然如要要测试接口;可以用SOAPUI工具测试;如图:
java webService之CXF的使用_第1张图片
上面测试了web service接口的开发;
接下来需要将UserService集成到SpringMVC中;
javaEE中CXF配置:
添加spring-cxf.xml配置文件到resource目录



    
    
    

    
    
    
        
        
    


javaEE项目中web.xml配置:其中要配置Spring-cxf.xml
 
 
        contextConfigLocation
        
            classpath*:spring/spring-core.xml
            classpath*:spring/spring-cxf.xml
        
    

        CXFServlet
        
            org.apache.cxf.transport.servlet.CXFServlet
        
        1
    
    
        CXFServlet
        /user/*
    


  • java webService之CXF的使用_第2张图片
  • 大小: 40.6 KB
  • 查看图片附件

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