Java SE 6 的 Web Service实例演示

时间:2021-07-25 17:21:04
1、服务:
package mustang.webservice.demo2;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Endpoint;

@WebService(name 
= "Calculator", targetNamespace = "http://java.webservice.com/calculator")
@SOAPBinding(style 
= SOAPBinding.Style.RPC)
public class Calculator {
    @WebMethod
    
public int add(int a, int b) {
        
return a+b;
    }
    
    
public static void main(String[] args){
        
// create and publish an endpoint
        Endpoint.publish("http://localhost:8080/calculator"new Calculator());        
    }
}
注意:确保端口不冲突!

2、客户端(一个接口、一个service的实现类和一个启动类):
接口:
package mustang.webservice.client.demo2;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

/**
 * This class was generated by the JAXWS SI. JAX-WS RI 2.0_02-b08-fcs Generated
 * source version: 2.0
 * 
 
*/
@WebService(name 
= "Calculator", targetNamespace = "http://java.webservice.com/calculator")
@SOAPBinding(style 
= SOAPBinding.Style.RPC)
public interface Calculator {

    @WebMethod
    @WebResult(partName 
= "return")
    
public int add(@WebParam(name = "arg0", partName = "arg0")int arg0, @WebParam(name = "arg1", partName = "arg1")int arg1);

}

Service实现类:
package mustang.webservice.client.demo2;

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

/**
 * This class was generated by the JAXWS SI. JAX-WS RI 2.0_02-b08-fcs Generated
 * source version: 2.0
 
*/
@WebServiceClient(name 
= "CalculatorService", targetNamespace = "http://java.webservice.com/calculator", wsdlLocation = "http://localhost:8080/calculator?wsdl")
public class CalculatorService extends Service {

    
private final static URL HELLOSERVICE_WSDL_LOCATION;

    
static {
        URL url 
= null;
        
try {
            url 
= new URL("http://localhost:8080/calculator?wsdl");
        } 
catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HELLOSERVICE_WSDL_LOCATION 
= url;
    }

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

    
public CalculatorService() {
        
super(HELLOSERVICE_WSDL_LOCATION, new QName(
                
"http://java.webservice.com/calculator""CalculatorService"));
    }

    
/**
     * 
     * 
@return
     
*/
    @WebEndpoint(name 
= "CalculatorPort")
    
public Calculator getCalculatorPort() {
        
return (Calculator) super.getPort(new QName(
                
"http://java.webservice.com/calculator""CalculatorPort"),
                Calculator.
class);
    }

}

启动类:
package mustang.webservice.client.demo2;

public class CalculatorClient {
        
public static void main(String args[]){
            
/**
             * Instantiate the generated Service
             
*/ 
            CalculatorService service 
= new CalculatorService();
            
            
/**
             * Get the port using port getter method generated in CaculatorService
             
*/
            Calculator calculatorProxy 
= service.getCalculatorPort();
            
            
/**
             * Invoke the remote method
             
*/
            
int result = calculatorProxy.add(1020);
            System.out.println(
"Sum of (10+20) = "+result);
        }
    }

先运行服务,然后启动客户端,ok!