用java sdk 写webservice 服务端

1. 写类文件

 

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.test;

import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * 2008-4-2
 * @author liuqing
 */
@WebService
public class UserWebservice {

    public String sayHello(@WebParam(name="username")String str) {
        System.out.println(" == Server");
        return "uml...." + str;
    }

}

 

2. 发布webservice 用服务端

 

 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.test;

import javax.xml.ws.Endpoint;

/**
 *
 * @author liuqing
 */
public class Test {
    
    public static void main(String args[]) {
        Endpoint.publish("http://localhost:7777/usb/userinfoes", new UserWebservice());
    }

}

  

 

  3. 发布后文件

 

 

<?xml version='1.0' encoding='UTF-8'?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2-hudson-740-. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2-hudson-740-. --><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.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://test.com/" name="UserWebserviceService">
<types>
<xsd:schema>
<xsd:import namespace="http://test.com/" schemaLocation="http://localhost:7777/usb/userinfoes?xsd=1" />
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello" />
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse" />
</message>
<portType name="UserWebservice">
<operation name="sayHello">
<input wsam:Action="http://test.com/UserWebservice/sayHelloRequest" message="tns:sayHello" />
<output wsam:Action="http://test.com/UserWebservice/sayHelloResponse" message="tns:sayHelloResponse" />
</operation>
</portType>
<binding name="UserWebservicePortBinding" type="tns:UserWebservice">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<operation name="sayHello">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="UserWebserviceService">
<port name="UserWebservicePort" binding="tns:UserWebservicePortBinding">
<soap:address location="http://localhost:7777/usb/userinfoes" />
</port>
</service>
</definitions>

 4. 使用jdk 自代的wsimport 生成客户端

 

  

D:\testwebservice>wsimport -keep -p com.webservice.fl.client http://localhost:77
77/usb/userinfoes?wsdl
parsing WSDL...


generating code...


compiling code...


D:\testwebservice>

 

   5. 客户端测试代码

 

  

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.test;

import com.webservice.fl.client.UserWebserviceService;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;

/**
 *
 * @author liuqing
 */
public class Test {
    
    public static void main(String args[]) throws Exception {
      //发布对应地址
      UserWebserviceService us = invokeWebService("http://localhost:7777/usb/userinfoes?wsdl");
      System.out.println( us.getUserWebservicePort().sayHello("uml..."));
      System.out.println(" " + UserWebserviceService.class.getResource(""));
    }
    
    public static UserWebserviceService invokeWebService(String wsdlAddress) throws MalformedURLException {
        
         URL url = new URL(UserWebserviceService.class.getResource("."),"http://localhost:7777/usb/userinfoes?wsdl");
         UserWebserviceService us = new UserWebserviceService(url,new QName("http://test.com/","UserWebserviceService"));
        return us;
    }

    public static UserWebserviceService invokeWebService(QName qname,String wsdlAddress) throws MalformedURLException {
        URL url = new URL(UserWebserviceService.class.getResource("."),wsdlAddress);
        UserWebserviceService us = new UserWebserviceService(url,qname);
        return us;
    }




}

  

 

 

 

 

 

 

你可能感兴趣的:(java,.net,webservice,SOAP,UML)