• CXF(1) 服务器端开发



    1、定义一个接口:

    使用个简单的注解。这里的注解是Java JDK自带的,而不是使用CXF的。

    package services;
    
    import java.util.List;
    
    import javax.jws.WebService;
    
    @WebService
    public interface Hello {
    	
    	public String sayHello(String name);
    
    	public List<Cat> getByUser(User user);
    	
    }
    


    2、 写一个实现类。

    endpointInterface 要写接口的全路径。serviceName自己定义啦

    这个注解也很简单,没什么好说的。

    package services.impl;
    
    import java.util.List;
    
    import javax.jws.WebService;
    
    import services.Cat;
    import services.Hello;
    import services.User;
    
    @WebService(endpointInterface="services.Hello",serviceName="HelloWS")
    public class HelloImpl implements Hello {
    
    	@Override
    	public String sayHello(String name) {
    		return "你好" + name;
    	}
    
    	@Override
    	public List<Cat> getByUser(User user) {
    		
    		return null;
    	}
    	
    
    }


    实体类Cat,User,不再写了,大家自己实现,做个测试而已。


    3、发布服务

    非常的简单。引入CXF的类库。

    不需要tomcat服务器神马的,因为cxf内置了jettyf服务器。

    一句话发布服务,方便吧~~~


    Endpoint.publish("http://10.5.82.195/hellows", hello);

    这个链接和后面的hello,没有太大联系。 hello是起的一个名字而已。真正访问的是这个链接。

    ip就是自己的主机IP了。


    import javax.xml.ws.Endpoint;
    
    import services.Hello;
    import services.impl.HelloImpl;
    
    public class ServerMain {
    
    	
    	public static void main(String[] args) {
    		
    		Hello hello = new HelloImpl();
    		
    		Endpoint.publish("http://10.5.82.195/hellows", hello);
    		
    		System.out.println("Web Service 发布成功!");
    	}
    
    }
    


    控制台:

    五月 20, 2013 4:36:15 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
    INFO: Creating Service {http://impl.services/}HelloWS from class services.Hello
    五月 20, 2013 4:36:16 下午 org.apache.cxf.endpoint.ServerImpl initDestination
    INFO: Setting the server's publish address to be http://10.5.82.195:80/hellows
    2013-05-20 16:36:16.600:INFO:oejs.Server:jetty-8.1.7.v20120910
    2013-05-20 16:36:16.648:INFO:oejs.AbstractConnector:Started SelectChannelConnector@10.5.82.195:80
    Web Service 发布成功!


    4、查看发布的wsdl

    地址栏:http://10.5.82.195/hellows?wsdl

    这里有几个命名空间是根据你的类的包名的逆置。

    为什么是逆置?因为包名是 域名的逆置, com.apache...    org.eclipse.jetty.....。 这样,命名空间再把包名倒过来,就变成 ....apache.com。

    我的包名起的太短了:package services.impl;     ->     targetNamespace="http://impl.services/"


    targetNamespace 这个词用的不好,其实就是给自己这个xml起的名字,一个命名空间。

    顺便说下 : targetNamespace ->  相当于Java里的  package; ( 就是为了唯一标识该命名空间。)

                xmlns     ->       相当于Java里的 import;   (引入其他的命名空间,使用前缀标识不同的命名空间。)

    (例:xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/", 这里导入的命名空间,肯定是在别处的schema里定义好的targetNamespace)

    (那么这里的 import相当与什么呢?就理解直接导入一个xml文件吧 )


    <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.services/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://services/" name="HelloWS" targetNamespace="http://impl.services/">
    <wsdl:import location="http://10.5.82.195/hellows?wsdl=Hello.wsdl" namespace="http://services/"></wsdl:import>
    <wsdl:binding name="HelloWSSoapBinding" type="ns1:Hello">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getByUser">
    <soap:operation soapAction="" style="document"/>
    <wsdl:input name="getByUser">
    <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output name="getByUserResponse">
    <soap:body use="literal"/>
    </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="sayHello">
    <soap:operation soapAction="" style="document"/>
    <wsdl:input name="sayHello">
    <soap:body use="literal"/>
    </wsdl:input>
    <wsdl:output name="sayHelloResponse">
    <soap:body use="literal"/>
    </wsdl:output>
    </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="HelloWS">
    <wsdl:port binding="tns:HelloWSSoapBinding" name="HelloImplPort">
    <soap:address location="http://10.5.82.195/hellows"/>
    </wsdl:port>
    </wsdl:service>
    </wsdl:definitions>


    主要描述信息不在这个文件。注意看,里面的import 导入的连接:http://10.5.82.195/hellows?wsdl=Hello.wsdl

    再打开这个链接:

    <wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://services/" name="Hello" targetNamespace="http://services/">
    <wsdl:types>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://services/" elementFormDefault="unqualified" targetNamespace="http://services/" version="1.0">
    <xs:element name="getByUser" type="tns:getByUser"/>
    <xs:element name="getByUserResponse" type="tns:getByUserResponse"/>
    <xs:element name="sayHello" type="tns:sayHello"/>
    <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
    <xs:complexType name="getByUser">
    <xs:sequence>
    <xs:element minOccurs="0" name="arg0" type="tns:user"/>
    </xs:sequence>
    </xs:complexType>
    <xs:complexType name="user">
    <xs:sequence>
    <xs:element name="id" type="xs:int"/>
    <xs:element minOccurs="0" name="name" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    <xs:complexType name="getByUserResponse">
    <xs:sequence>
    <xs:element maxOccurs="unbounded" minOccurs="0" name="return" type="tns:cat"/>
    </xs:sequence>
    </xs:complexType>
    <xs:complexType name="cat">
    <xs:sequence/>
    </xs:complexType>
    <xs:complexType name="sayHello">
    <xs:sequence>
    <xs:element minOccurs="0" name="arg0" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    <xs:complexType name="sayHelloResponse">
    <xs:sequence>
    <xs:element minOccurs="0" name="return" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:schema>
    </wsdl:types>
    <wsdl:message name="sayHelloResponse">
    <wsdl:part element="ns1:sayHelloResponse" name="parameters"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="sayHello">
    <wsdl:part element="ns1:sayHello" name="parameters"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="getByUser">
    <wsdl:part element="ns1:getByUser" name="parameters"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="getByUserResponse">
    <wsdl:part element="ns1:getByUserResponse" name="parameters"></wsdl:part>
    </wsdl:message>
    <wsdl:portType name="Hello">
    <wsdl:operation name="getByUser">
    <wsdl:input message="ns1:getByUser" name="getByUser"></wsdl:input>
    <wsdl:output message="ns1:getByUserResponse" name="getByUserResponse"></wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="sayHello">
    <wsdl:input message="ns1:sayHello" name="sayHello"></wsdl:input>
    <wsdl:output message="ns1:sayHelloResponse" name="sayHelloResponse"></wsdl:output>
    </wsdl:operation>
    </wsdl:portType>
    </wsdl:definitions>


  • 相关阅读:
    Docker安装模拟MySQL集群
    Java中类似PHP的var_dump()
    同样的sql语句在数据库中能查出结果,但是在java springboot项目中,查不出来
    import org.junit.Test 导入失败 @Test JUnit4单元测试找不到
    解决用eclipse spring start project 自动创建项目时pom文件错误
    Laravel 队列的简单使用例子
    跨库触发器
    MySQL 主从复制 详细实例讲解 与 常见错误解决方法
    2014年末总结:对大数据处理的一点思考
    Range
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3089692.html
Copyright © 2020-2023  润新知