WebService之JDK中wsimport命令

Posted jiapeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于WebService之JDK中wsimport命令的主要内容,如果未能解决你的问题,请参考以下文章

wsimport命令生成webService java客户端代码

基于jdk wsimport工具訪问外部webservice

如何用IDEA一步一步开发WebService服务器端

如何用IDEA一步一步开发WebService服务器端

webservice wsimport 报错

根据wsimport命令生成webservice客户端