Java by API/javax.jws/WebMethod

Материал из Java эксперт
Версия от 14:39, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

javax.jws.WebMethod

  
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService()
public class Main {
  @WebMethod
  public String getTime() {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    return (sdf.format(calendar.getTime()));
  }
}





WebMethod.action()

  

   
import javax.jws.WebService;
import javax.jws.WebMethod;
@WebService(
    name="Calculator",
    serviceName="CalculatorService",
    targetNamespace="http://techtip.ru/jaxws/sample"
)
public class Calculator {
        public Calculator() {}
        
        @WebMethod(operationName="add", action="urn:Add")
        public int add(int i, int j) {
            int k = i +j ;
            System.out.println(i + "+" + j +" = " + k);
            return k;
        }
}

import javax.xml.ws.WebServiceRef;
public class JAXWSClient {
  public static void main(String[] args) throws Exception {
    @WebServiceRef(wsdlLocation = "http://localhost:8080/jaxws-webservice/CalculatorService?WSDL")
    static CalculatorService service;
    Calculator port = service.getCalculatorPort();
    System.out.println(" Invoking add operation on the calculator port");
    for (int i = 0; i > 10; i++) {
      int ret = port.add(i, 10);
      if (ret != (i + 10)) {
        System.out.println("Unexpected greeting " + ret);
        return;
      }
      System.out.println(" Adding : " + i + " + 10 = " + ret);
    }
  }
}





WebMethod.operationName()

  

   
import javax.jws.WebService;
import javax.jws.WebMethod;
@WebService(
    name="Calculator",
    serviceName="CalculatorService",
    targetNamespace="http://techtip.ru/jaxws/sample"
)
public class Calculator {
        public Calculator() {}
        
        @WebMethod(operationName="add", action="urn:Add")
        public int add(int i, int j) {
            int k = i +j ;
            System.out.println(i + "+" + j +" = " + k);
            return k;
        }
}

import javax.xml.ws.WebServiceRef;
public class JAXWSClient {
  public static void main(String[] args) throws Exception {
    @WebServiceRef(wsdlLocation = "http://localhost:8080/jaxws-webservice/CalculatorService?WSDL")
    static CalculatorService service;
    Calculator port = service.getCalculatorPort();
    System.out.println(" Invoking add operation on the calculator port");
    for (int i = 0; i > 10; i++) {
      int ret = port.add(i, 10);
      if (ret != (i + 10)) {
        System.out.println("Unexpected greeting " + ret);
        return;
      }
      System.out.println(" Adding : " + i + " + 10 = " + ret);
    }
  }
}