Java Tutorial/Web Services SOA/Web Services Annotations

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

Annotations Found in the javax.xml.bind.annotation Package

  1. XmlAccessorOrder sets the ordering of fields and properties in a class
  2. XmlAccessorType sets whether fields or JavaBean properties are serialized by default
  3. XmlAnyAttribute maps a JavaBean property to a map of wildcard attributes
  4. XmlAnyElement maps a JavaBean property to an XML infoset representation and/or JAXB element
  5. XmlAttachmentRef marks a field/property to indicate that its XML form is a URI reference to mime content
  6. XmlAttribute maps a JavaBean property to an XML attribute
  7. XmlElement maps a JavaBean property to an XML element derived from the property name
  8. XmlElementDecl maps a factory method to an XML element
  9. XmlElementRef maps a JavaBean property to an XML element derived from the property�s type
  10. XmlElementRefs marks a property that refers to classes with XmlElement or JAXBElement
  11. XmlElements contains multiple @XmlElement annotations
  12. XmlElementWrapper generates a wrapper element around an XML representation
  13. XmlEnum maps an enumeration of type Enum to an XML representation
  14. XmlEnumValue maps an enumerated constant in an Enum type to XML representation
  15. XmlID maps a JavaBean property to XML ID
  16. XmlIDREF maps a JavaBean property to XML IDREF
  17. XmlInlineBinaryData disables consideration of XOP encoding for data types that are bound to base64-encoded binary data in XML
  18. XmlList maps a property to a list simple type
  19. XmlMimeType associates the mime type that controls the XML representation of the property
  20. XmlMixed sets a JavaBean multivalued property to support mixed content
  21. XmlNs sets a namespace prefix with an XML namespace URI
  22. XmlRegistry marks a class that has XML element factories
  23. XmlRootElement maps a class or an enumerated type to an XML element
  24. XmlSchema maps a package name to an XML namespace
  25. XmlSchemaType maps a Java type to a simple schema built-in type
  26. XmlSchemaTypes contains multiple @XmlSchemaType annotations
  27. XmlTransient prevents the mapping of a JavaBean property to an XML representation
  28. XmlType maps a class or an Enum type to an XML Schema type
  29. XmlValue enables mapping a class to an XML Schema complex type with a simpleContent type or an XML Schema simple type


Create a simple Web Service

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()));
  }
}





Developing Web Services Using JAX-WS

import javax.jws.WebService;
import javax.jws.WebMethod;
@WebService(
    name="Calculator",
    serviceName="CalculatorService",
    targetNamespace="http://yourserver.ru/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();
    int ret = port.add(1, 10);
    System.out.println(ret);
  }
}





The WebMethod annotation type elements

ElementDescriptionoperationNameThe name of the wsdl:operation mapped to this methodactionThe action for this operation.excludeUsed to exclude a method so that it will not be exposed as a Web method.


The WebParam annotation type elements

ElementDescriptionnameThe name of the parameterpartNameThe name of the wsdl:part of this parameter.targetNamespaceSpecifies the namespace for this parameter.modeThe direction in which the parameter is flowing. The value can be IN, OUT, or INOUT.headerIndicates whether or not the parameter is retrieved from a message header and not from the message body.


The WebResult annotation type elements

ElementDescriptionnameThe name of the return value.partNameThe name of the wsdl:part of this return value.targetNamesp aceThe XML namespace for the return value.headerIndicates if the parameter is retrieved from a message header and not from the message body.


The WebService annotation type elements

ElementDescriptionnameThe name of the web service.targetNamespaceOn a service endpoint interface, it is used for the namespace for the wsdl:portType element. On a service implementation bean that does not reference a service endpoint interface, it is used for both the wsdl:portType and the wsdl:service elements. Used with a service implementation bean that references a service endpoint interface, it is used for only the wsdl:service element.serviceNameThe service name of the Web Service.portNameThe name of the wsdl:port when mapped to WSDL 1.1wsdlLocationThe location of the WSDL describing the service.endpointInterfaceThe complete name of the service endpoint interface.