zoukankan      html  css  js  c++  java
  • WebService之JDK中wsimport命令

    1、编写WebService类,使用@WebService注解

    package test;
    
    import javax.jws.WebService;
    
    @WebService
    public class HelloServiceImpl{
    
        public String say(String name) {
            return "Hello "+name;
        }
    }
    WebService类

    2、使用main方法发布WebService

    package test;
    
    import javax.xml.ws.Endpoint;
    
    public class Publisher {
        public static void main(String[] args) {
            Endpoint.publish("http://192.168.0.103:8088/hello", new HelloServiceImpl());
        }
    }
    main方法发布WebService

    3、发布成功后,访问发布地址+?wsdl获取网络服务描述语言,其中节点内tns冒号表示targetNameSpace,指向引用节点

    This XML file does not appear to have any style information associated with it. The document tree is shown below.
    <!--
     Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
    -->
    <!--
     Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. 
    -->
    <definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://test/" name="HelloServiceImplService">
    <types>
    <xsd:schema>
    <xsd:import namespace="http://test/" schemaLocation="http://192.168.0.103:8088/hello?xsd=1"/>
    </xsd:schema>
    </types>
    <message name="say">
    <part name="parameters" element="tns:say"/>
    </message>
    <message name="sayResponse">
    <part name="parameters" element="tns:sayResponse"/>
    </message>
    <portType name="HelloServiceImpl">
    <operation name="say">
    <input wsam:Action="http://test/HelloServiceImpl/sayRequest" message="tns:say"/>
    <output wsam:Action="http://test/HelloServiceImpl/sayResponse" message="tns:sayResponse"/>
    </operation>
    </portType>
    <binding name="HelloServiceImplPortBinding" type="tns:HelloServiceImpl">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="say">
    <soap:operation soapAction=""/>
    <input>
    <soap:body use="literal"/>
    </input>
    <output>
    <soap:body use="literal"/>
    </output>
    </operation>
    </binding>
    <service name="HelloServiceImplService">
    <port name="HelloServiceImplPort" binding="tns:HelloServiceImplPortBinding">
    <soap:address location="http://192.168.0.103:8088/hello"/>
    </port>
    </service>
    </definitions>
    WSDL

    4、使用JDK中wsimport生成WebService客户端Java类

     wsimport -s . -p com.hjp.stub http://192.168.0.103:8088/hello?wsdl -Xnocompile

    -s后面用点表示在当前目录下,-p后面第一个参数表示生成类的包,第二个参数是WebService服务的wsdl,-Xnocompile表示不需要编译,如果去掉-Xnocompile会有编译的class文件

    5、将第四步生成好的Java文件,复制到客户端项目中,编写客户端测试代码

    package com.hjp.client;
    
    import com.hjp.stub.HelloServiceImpl;
    import com.hjp.stub.HelloServiceImplService;
    
    public class Client {
    
        public static void main(String[] args){
            //创建服务访问点集合对象
            HelloServiceImplService helloServiceImplService=new HelloServiceImplService();
            //获得服务点绑定的类
            HelloServiceImpl helloService=helloServiceImplService.getHelloServiceImplPort();
            //调用服务端方法
            String returnstr=helloService.say("小明");
            System.out.println(returnstr);
        }
    
    }
    客户端代码

     6、扩展WebService内参数

    如果想修改WSDL内节点名称,可以设置@WebService(在类上)、@WebMethod(在方法上)、@WebParam(在参数前)内name参数

    如果排除其中某方法,使用@WebMethod内exclude=true

  • 相关阅读:
    高级程序员和普通程序员有哪些区别?
    自学编程需要注意什么?
    你是如何转行的?转行容易吗?
    什么是VueCLI3
    强制缓存和协商缓存有什么区别
    npm与package.json
    最高效的学习方法——逆向学习法
    html space空格符
    in typeof instanceof ===这些运算符有什么作用
    教你如何“快速”激活IntelliJ IDEA 工具
  • 原文地址:https://www.cnblogs.com/hujiapeng/p/6352268.html
Copyright © 2011-2022 走看看