在IDEA中生成webservice客户端

一、前言

第一次接触webservice,从采坑到采坑,算是了解了一些,明白了一些;生成webservice的方法有很多种(实现方式有CXF、Axis等等),webservice 是一种基于web的应用程序,可以使用多种编程语言实现。JAVA 程序猿当然选择最快,最有效的方法。

二、使用IDEA创建webservice客户端

我认为通过IDE生成是有效的,我的目的只是能够生成且调用。目的----方法(适配)
1)准备IDEA
首先生成的webservice客户端java代码,你可以单独创建一个项目,或者把他当时是某个项目里的一个service。(如果单独创建一个项目,创建一个简单的javaproject就能够使用webservice客户端)。
我是在我的SpringBoot项目中调用webservice(作为一个service),所以略过创建项目的过程。在项目中可以创建一个新的包来放生成的webservice客户端java代码,然后选中这个包,右击在idea的菜单中的最下面(有点难找,图片还被水印挡住了,但这些都不重要)有【webservice】点开有【Generate Java code from WSDl】 ,也就是通过wsdl生成java代码。如下图所示:
在IDEA中生成webservice客户端_第1张图片
2) 准备WSDL
这个可以通过地址或者本地WSDL文件,这里可能还有一个坑,就是IDEA的代理设置。点击进去以后,按下图操作(字多懒得打),如图二所示:
在IDEA中生成webservice客户端_第2张图片
说明一下,这些生成方式大同小异,我的目的只是能够用,我看了通过cxf生成的和通过普片生成的java code 是差不多一样的,但是使用CSF需要一些依赖,否则IDEA无法生成,先把依赖站上把。



    org.apache.cxf
    cxf-bundle
 

3)设置
第一个 webservice wsdl url ; 是wsdl 文件的位置,可以是本地电脑或者网络上的。
本地的选择浏览文件,找到就行,网络的你可以将地址放在浏览器中看能不能打开。
第二个: output path : java代码的输出路径
第三个; package prefix : 包路径前缀
关于第二个和第三个,在一开始选择的时候选择好要生成的位置就可以,会自动配置。
第四个就是选择的生成方式,图片中的这种挺方便的。

最后一步点击Ok,等待生成就好了。地址不合法的,按钮是灰色的,不可点,基于网络的WSDL 如果无法生成,检查IDEA中代理设置(setting中搜索proxy)。最后生成的java代码如图三所示:
在IDEA中生成webservice客户端_第3张图片
删除了packageinfo类 \生成的.wsdl文件和。class文件。然后没个版本可能生成的代码又差异,但是最重要的就是 图片中所示的前三个类。

明天再写怎么用和设置超时时间吧,好困。
设置webservice超时时间,地址
设置webservice的超时时间

把生成的代码粘上,可以导入自己的包内用,但是需要修改地址, 敏感位置用xxx代替了;

package com.springcloud.xxx.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 3.2.4
 * 2018-06-06T09:27:30.288+08:00
 * Generated source version: 3.2.4
 *这个类里面那些targetNamespace   是根据实际情况修改的,和类的地址需要对应上,需要根据自己的情况修改
 */
@WebService(targetNamespace = "http://server.xxx.com/", name = "CallService")
@XmlSeeAlso({ObjectFactory.class})
public interface CallService {

    @WebMethod
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "invoke", targetNamespace = "http://server.xxx.com/", className = "com.springcloud.webservice.Invoke")
    @ResponseWrapper(localName = "invokeResponse", targetNamespace = "http://server.xxx.com", className = "com.springcloud.webservice.InvokeResponse")
    public String invoke(
            @WebParam(name = "arg0", targetNamespace = "")
                    String arg0,
            @WebParam(name = "arg1", targetNamespace = "")
                    String arg1,
            @WebParam(name = "arg2", targetNamespace = "")
                    String arg2,
            @WebParam(name = "arg3", targetNamespace = "")
                    String arg3,
            @WebParam(name = "arg4", targetNamespace = "")
                    String arg4
    );
}

package com.springcloud.slebbatch.webservice;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * This class was generated by Apache CXF 3.2.4
 * 2018-06-06T09:27:30.399+08:00
 * Generated source version: 3.2.4
 * 这个工厂类中的静态代码块可能会有不同,下篇博客会用到。
 */
@WebServiceClient(name = "CallServiceFactory",
                  wsdlLocation = "http://110.156.13.57:7080/services/CallService?wsdl",
                  targetNamespace = "http://server.xxx.com/")
public class CallServiceFactory extends Service {

    public static URL WSDL_LOCATION;

    public final static QName SERVICE = new QName("http://server.xxx.com/", "CallServiceFactory");
    public final static QName CallServicePort = new QName("http://server.xxx.com/", "CallServicePort");
    public static void setURL(String wsdl) {
        URL url = null;
        try {
            url = new URL(wsdl);
        } catch (MalformedURLException e) {
            java.util.logging.Logger.getLogger(CallServiceFactory.class.getName())
                .log(java.util.logging.Level.INFO,
                     "Can not initialize the default wsdl from {0}", "http://110.156.13.57:7080/services/CallService?wsdl");
        }
        WSDL_LOCATION = url;
    }

    public CallServiceFactory(URL wsdlLocation) {
        super(wsdlLocation, SERVICE);
    }

    public CallServiceFactory(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public CallServiceFactory() {
        super(WSDL_LOCATION, SERVICE);
    }

    public CallServiceFactory(WebServiceFeature ... features) {
        super(WSDL_LOCATION, SERVICE, features);
    }

    public CallServiceFactory(URL wsdlLocation, WebServiceFeature ... features) {
        super(wsdlLocation, SERVICE, features);
    }

    public CallServiceFactory(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
        super(wsdlLocation, serviceName, features);
    }
    
    /**
     *
     * @return
     *     returns CallService
     */
    @WebEndpoint(name = "CallServicePort")
    public CallService getCallServicePort() {
        return super.getPort(CallServicePort, CallService.class);
    }

    /**
     *
     * @param features
     *     A list of {@link WebServiceFeature} to configure on the proxy.  Supported features not in the features parameter will have their default values.
     * @return
     *     returns CallService
     */
    @WebEndpoint(name = "CallServicePort")
    public CallService getCallServicePort(WebServiceFeature... features) {
        return super.getPort(CallServicePort, CallService.class, features);
    }
}


package com.springcloud.webservice;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;


/**
 * 

invoke complex type的 Java 类。 * *

以下模式片段指定包含在此类中的预期内容。 * *

 * <complexType name="invoke">
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <sequence>
 *         <element name="arg0" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         <element name="arg1" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         <element name="arg2" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         <element name="arg3" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         <element name="arg4" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       </sequence>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * 
* * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "invoke", propOrder = { "arg0", "arg1", "arg2", "arg3", "arg4" }) public class Invoke { protected String arg0; protected String arg1; protected String arg2; protected String arg3; protected String arg4; /** * 获取arg0属性的值。 * * @return * possible object is * {@link String } * */ public String getArg0() { return arg0; } /** * 设置arg0属性的值。 * * @param value * allowed object is * {@link String } * */ public void setArg0(String value) { this.arg0 = value; } /** * 获取arg1属性的值。 * * @return * possible object is * {@link String } * */ public String getArg1() { return arg1; } /** * 设置arg1属性的值。 * * @param value * allowed object is * {@link String } * */ public void setArg1(String value) { this.arg1 = value; } /** * 获取arg2属性的值。 * * @return * possible object is * {@link String } * */ public String getArg2() { return arg2; } /** * 设置arg2属性的值。 * * @param value * allowed object is * {@link String } * */ public void setArg2(String value) { this.arg2 = value; } /** * 获取arg3属性的值。 * * @return * possible object is * {@link String } * */ public String getArg3() { return arg3; } /** * 设置arg3属性的值。 * * @param value * allowed object is * {@link String } * */ public void setArg3(String value) { this.arg3 = value; } /** * 获取arg4属性的值。 * * @return * possible object is * {@link String } * */ public String getArg4() { return arg4; } /** * 设置arg4属性的值。 * * @param value * allowed object is * {@link String } * */ public void setArg4(String value) { this.arg4 = value; } }

package com.springcloud.webservice;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


/**
 * 

invokeResponse complex type的 Java 类。 * *

以下模式片段指定包含在此类中的预期内容。 * *

 * <complexType name="invokeResponse">
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <sequence>
 *         <element name="return" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *       </sequence>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * 
* * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "invokeResponse", propOrder = { "_return" }) public class InvokeResponse { @XmlElement(name = "return") protected String _return; /** * 获取return属性的值。 * * @return * possible object is * {@link String } * */ public String getReturn() { return _return; } /** * 设置return属性的值。 * * @param value * allowed object is * {@link String } * */ public void setReturn(String value) { this._return = value; } }

package com.springcloud.webservice;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;


/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the com.sinosoft.ws.server.manage package. 
 * 

An ObjectFactory allows you to programatically * construct new instances of the Java representation * for XML content. The Java representation of XML * content can consist of schema derived interfaces * and classes representing the binding of schema * type definitions, element declarations and model * groups. Factory methods for each of these are * provided in this class. * */ @XmlRegistry public class ObjectFactory { private final static QName _Invoke_QNAME = new QName("http:/.server.xx.com/", "invoke"); private final static QName _InvokeResponse_QNAME = new QName("http://server.xxx.com/", "invokeResponse"); /** * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.sinosoft.ws.server.manage * */ public ObjectFactory() { } /** * Create an instance of {@link Invoke } * */ public Invoke createInvoke() { return new Invoke(); } /** * Create an instance of {@link InvokeResponse } * */ public InvokeResponse createInvokeResponse() { return new InvokeResponse(); } /** * Create an instance of {@link JAXBElement }{@code <}{@link Invoke }{@code >}} * */ @XmlElementDecl(namespace = "http://server.xxx.com/", name = "invoke") public JAXBElement createInvoke(Invoke value) { return new JAXBElement(_Invoke_QNAME, Invoke.class, null, value); } /** * Create an instance of {@link JAXBElement }{@code <}{@link InvokeResponse }{@code >}} * */ @XmlElementDecl(namespace = "http://manage.server.ws.sinosoft.com/", name = "invokeResponse") public JAXBElement createInvokeResponse(InvokeResponse value) { return new JAXBElement(_InvokeResponse_QNAME, InvokeResponse.class, null, value); } }

你可能感兴趣的:(【,后端,】,idea,webservice,生成webservice)