JAX-WS(二)通过wsimport解析wsdl

1.使用wsimport命令将wsdl转换为本地的java接口
wsimport 是java提供的命令,专门用来对wsdl文件进行解析,并转换为本地的java文件
命令:
wsimport -d E:\technology-hqh\proj\webservice\JAX-WS\wsimport_01 -keep http://localhost:8888/numberService?wsdl
说明:
-d 指名生成文件的输出路径
-keep 生成的文件除了class文件,还包括java源文件
-verbose 生成的详细信息
2.访问webservice
本地创建一个新的项目,将wsimport生成的java文件拷贝到src中
由wsimport生成的文件中,会生成一个接口和一个实现类
实现类中提供了一个getXXXport(),用来返回具体的实现类
接口---实现类【多态】
IMyService接口

package com.hqh.service;

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 the JAX-WS RI.
 * JAX-WS RI 2.1.6 in JDK 6
 * Generated source version: 2.1
 * 
 */
@WebService(name = "IMyService", targetNamespace = "http://service.hqh.com/")
@XmlSeeAlso({
    ObjectFactory.class
})
public interface IMyService {


    /**
     * 
     * @param arg1
     * @param arg0
     * @return
     *     returns int
     */
    @WebMethod
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "plus", targetNamespace = "http://service.hqh.com/", className = "com.hqh.service.Plus")
    @ResponseWrapper(localName = "plusResponse", targetNamespace = "http://service.hqh.com/", className = "com.hqh.service.PlusResponse")
    public int plus(
        @WebParam(name = "arg0", targetNamespace = "")
        int arg0,
        @WebParam(name = "arg1", targetNamespace = "")
        int arg1);

    /**
     * 
     * @param arg1
     * @param arg0
     * @return
     *     returns int
     */
    @WebMethod
    @WebResult(targetNamespace = "")
    @RequestWrapper(localName = "minus", targetNamespace = "http://service.hqh.com/", className = "com.hqh.service.Minus")
    @ResponseWrapper(localName = "minusResponse", targetNamespace = "http://service.hqh.com/", className = "com.hqh.service.MinusResponse")
    public int minus(
        @WebParam(name = "arg0", targetNamespace = "")
        int arg0,
        @WebParam(name = "arg1", targetNamespace = "")
        int arg1);

}


MyServiceImplService实现类

package com.hqh.service;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;
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;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.6 in JDK 6
 * Generated source version: 2.1
 * 
 */
@WebServiceClient(name = "MyServiceImplService", targetNamespace = "http://service.hqh.com/", wsdlLocation = "http://localhost:8888/numberService?wsdl")
public class MyServiceImplService
    extends Service
{

    private final static URL MYSERVICEIMPLSERVICE_WSDL_LOCATION;
    private final static Logger logger = Logger.getLogger(com.hqh.service.MyServiceImplService.class.getName());

    static {
        URL url = null;
        try {
            URL baseUrl;
            baseUrl = com.hqh.service.MyServiceImplService.class.getResource(".");
            url = new URL(baseUrl, "http://localhost:8888/numberService?wsdl");
        } catch (MalformedURLException e) {
            logger.warning("Failed to create URL for the wsdl Location: 'http://localhost:8888/numberService?wsdl', retrying as a local file");
            logger.warning(e.getMessage());
        }
        MYSERVICEIMPLSERVICE_WSDL_LOCATION = url;
    }

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

    public MyServiceImplService() {
        super(MYSERVICEIMPLSERVICE_WSDL_LOCATION, new QName("http://service.hqh.com/", "MyServiceImplService"));
    }

    /**
     * 这个方法就可以方便的返回一个实现类来让接口进行引用!!!
     * @return
     *     returns IMyService
     */
    @WebEndpoint(name = "MyServiceImplPort")
    public IMyService getMyServiceImplPort() {
        return super.getPort(new QName("http://service.hqh.com/", "MyServiceImplPort"), IMyService.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns IMyService
     */
    @WebEndpoint(name = "MyServiceImplPort")
    public IMyService getMyServiceImplPort(WebServiceFeature... features) {
        return super.getPort(new QName("http://service.hqh.com/", "MyServiceImplPort"), IMyService.class, features);
    }

}



访问外部服务
package com.hqh.service.test;

import com.hqh.service.IMyService;
import com.hqh.service.MyServiceImplService;

public class VisitRemotServer {
	public static void main(String[] args) {
		//wsimport生成的实现类
		MyServiceImplService myServiceImpl = new MyServiceImplService();
		
		//通过实现类中的getXXXPort()返回一个实现给接口
		IMyService iService = myServiceImpl.getMyServiceImplPort();
		
		//接口调用方法(服务提供者中的方法将被调用执行并返回结果)
		int retPlus = iService.plus(1, 2);
		System.out.println(retPlus);
	}
}

你可能感兴趣的:(import)