Java Tutorial/Web Services SOA/Web Services Annotations
Содержание
Annotations Found in the javax.xml.bind.annotation Package
- XmlAccessorOrder sets the ordering of fields and properties in a class
- XmlAccessorType sets whether fields or JavaBean properties are serialized by default
- XmlAnyAttribute maps a JavaBean property to a map of wildcard attributes
- XmlAnyElement maps a JavaBean property to an XML infoset representation and/or JAXB element
- XmlAttachmentRef marks a field/property to indicate that its XML form is a URI reference to mime content
- XmlAttribute maps a JavaBean property to an XML attribute
- XmlElement maps a JavaBean property to an XML element derived from the property name
- XmlElementDecl maps a factory method to an XML element
- XmlElementRef maps a JavaBean property to an XML element derived from the property�s type
- XmlElementRefs marks a property that refers to classes with XmlElement or JAXBElement
- XmlElements contains multiple @XmlElement annotations
- XmlElementWrapper generates a wrapper element around an XML representation
- XmlEnum maps an enumeration of type Enum to an XML representation
- XmlEnumValue maps an enumerated constant in an Enum type to XML representation
- XmlID maps a JavaBean property to XML ID
- XmlIDREF maps a JavaBean property to XML IDREF
- XmlInlineBinaryData disables consideration of XOP encoding for data types that are bound to base64-encoded binary data in XML
- XmlList maps a property to a list simple type
- XmlMimeType associates the mime type that controls the XML representation of the property
- XmlMixed sets a JavaBean multivalued property to support mixed content
- XmlNs sets a namespace prefix with an XML namespace URI
- XmlRegistry marks a class that has XML element factories
- XmlRootElement maps a class or an enumerated type to an XML element
- XmlSchema maps a package name to an XML namespace
- XmlSchemaType maps a Java type to a simple schema built-in type
- XmlSchemaTypes contains multiple @XmlSchemaType annotations
- XmlTransient prevents the mapping of a JavaBean property to an XML representation
- XmlType maps a class or an Enum type to an XML Schema type
- 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.