• Web Service (四) 手动发布Web Service接口-CXF与Spring集成


           当我们发布完Web Service接口之后有两种方式可以调用Web service服务,一种是通过动态客户端方式,另一种是引用服务端的接口,引用服务端接口的方式对于客户端同服务器端耦合比较大,而使用WSDL的方式客户端不知道服务端的存在就可以调用服务器的方法。

           下面是项目的结构图:

           1.Web Service发布项目



    2.编写服务端接口类以及实现类,如下,同上一篇自动发布接口,多了一个注解@WebService

    package com.test.webservice;
    
    import javax.jws.WebService;
    
    @WebService
    public interface IHelloWorld {
    	public String sayHello(User user);
    	public String sayHello1();
    }
    

    package com.test.webservice;
    
    import javax.jws.WebMethod;
    import javax.jws.WebService;
    
    @WebService(endpointInterface="com.test.webservice.IHelloWorld",serviceName="helloService123")
    public class HelloWorldImpl implements IHelloWorld {
    
    	@Override
    	public String sayHello(User user) {
    		System.out.println("开始调用Web Service method:sayHello()");
    		return user.getUsername()+"lilongsheng";
    	}
    
    	@Override
    	@WebMethod
    	public String sayHello1() {
    		System.out.println("开始调用Web Service method:sayHello1()");
    		return "lilongsheng1";
    	}
    
    }
    
           在实现类上面还可以加上SOAP注解,如

    @SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
           表示Web Service方法传递数据的类型、编码等设置。

    参考:http://dlc-cdn.sun.com/jdk/jdk-api-localizations/jdk-api-zh-cn/publish/1.6.0/html/zh_CN/api/javax/jws/soap/SOAPBinding.html

    3.Spring配置文件配置

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.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:endpoint id="helloService456" implementor="com.test.webservice.HelloWorldImpl" address="/helloService789"/>
    	
    	
    </beans>
    

    4.客户端测试代码

    package com.test.webservice;
    
    //import org.apache.cxf.endpoint.Client;
    import org.apache.cxf.endpoint.Client;
    import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
    
    public class testClient {
    	public static void main(String[] args) {  
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();  
            //创建客户端
            Client client = dcf.createClient("http://192.168.24.82:8080/Web_Service_Spring/ws/helloService789?wsdl");  
            Object[] objects;  
            try {  
            	User user=new User();
            	user.setUsername("longsheng");
                objects = client.invoke("sayHello",user);  
                //输出调用结果  
                System.out.println(objects[0].toString());  
            } catch (Exception e) {  
                e.printStackTrace();  
            }   
        }  
    }
    

           上面传参为传实体,实体需要实现序列化接口才能先通过网络传输。Web service在系统间以及系统交互上用的很广泛。


  • 相关阅读:
    Blob隐藏真实路径
    Vue原理笔记3
    Vue原理笔记2
    Vue双向绑定原理
    Vue原理笔记1
    MVC、MVP、MVVM
    Go语言学习之-带分割符的文件转excel
    IBMMQ之工具类
    IBMMQ之取发文件
    JAVA之我的公共部分测试调用
  • 原文地址:https://www.cnblogs.com/lilongsheng1125/p/4978502.html
Copyright © 2020-2023  润新知