Vous êtes sur la page 1sur 53

Java™ Web Services

Development Using
Annotations
Brian Zotter
Staff Engineer
BEA Systems
http://www.bea.com
TS-7964
2005 JavaOne SM Conference | Session TS-7964 1
Instant Web Services

Learn how to use Java™ technology-


based Web Services (“Java Web
Services”) Metadata to quickly develop
Enterprise Web Services.

2005 JavaOneSM Conference | Session TS-7964 | 2


Current Web Services Development

• Why is Java Web Service development


so difficult?
• Complicated APIs
• Manual creation of deployment descriptors
• Lack of tool support

2005 JavaOneSM Conference | Session TS-7964 | 3


Agenda

• JSR 181
• Development Models
• Processor
• Runtime
• Annotations
• Reference Implementation
• Next Steps
• Summary
• Q&A
2005 JavaOneSM Conference | Session TS-7964 | 4
Agenda

• JSR 181
• Development Models
• Processor
• Runtime
• Annotations
• Reference Implementation
• Next Steps
• Summary
• Q&A
2005 JavaOneSM Conference | Session TS-7964 | 5
JSR 181

• Defines an annotated Java language syntax


for programming Web Service applications
• Allows the developer to concentrate on the
business logic of the service
• Defines a standard for building and deploying Web
Services without requiring knowledge
and implementation of generalized APIs and
deployment descriptors

2005 JavaOneSM Conference | Session TS-7964 | 6


Exposes this
Hello World class as a Web
Service

@WebService
public class HelloWorld { Exposes this
method as a Web
Service operation
@WebMethod
public String hello(){
return “Hello World!”;
}
}

2005 JavaOneSM Conference | Session TS-7964 | 7


Development Modes

• Start with Java technology


• Exposing a Java technology service
• Only required mode
• Start with WSDL
• Implementing a public contract
• Start with Java technology and WSDL
• Implementation must provide feedback if Java
technology source and WSDL are not in sync

2005 JavaOneSM Conference | Session TS-7964 | 8


JSR 181 Processor

• Produces a “runnable” Web Service


• Performs error checking
• Checks not caught by the compiler
• Processing may happen at any time
• At build time
• In a tool
• During deployment
• Left to the implementation

2005 JavaOneSM Conference | Session TS-7964 | 9


Processing Model
Java™ 2 Platform, Enterprise Edition (J2EE™)
Web Service generation
Java Web Service J2SE 1.5 .class file
(JWS) Compiler containing
Source File annotations
Syntax and
Type Checking

JSR-181 Processor

Semantic Checking
Deployment Artifact
Generation

J2EE .class files for


WSDL File Deployment service and related
Descriptors types

J2EE 1.4 Deployment Unit (.ear or .war)

2005 JavaOneSM Conference | Session TS-7964 | 10


JSR 181 Runtime

• Handles runtime behavior


• Exposes a “processed” Web Service at a URL
• Provides access to the service’s WSDL
• May be the same component as the processor

2005 JavaOneSM Conference | Session TS-7964 | 11


Agenda

• JSR 181
• Development Models
• Processor
• Runtime
• Annotations
• Reference Implementation
• Next Steps
• Summary
• Q&A
2005 JavaOneSM Conference | Session TS-7964 | 12
Annotations

• JSR 175
• Purpose
• Java technology to WSDL Mapping
• Binding
• Processing(Handlers)
• Defines smart defaults
(Configuration by exception)

2005 JavaOneSM Conference | Session TS-7964 | 13


Annotations

• WebService
• OneWay
• WebParam
• WebResult
• SOAPBinding
• HandlerChain
• SoapMessageHandler(SoapMessageHandlers)

2005 JavaOneSM Conference | Session TS-7964 | 14


WebService

• Marks a Java class as implementing a Web Service,


or a Java technology interface as defining a
Web Service interface
• name
• Sets the name of the WSDL <portType> for this service
• serviceName
• Sets the name of the WSDL <service> generated for this service
• targetNamespace
• Sets the XML namespace used for WSDL and schema elements
defined from this service
• wsdlLocation
• The location of a pre-defined WSDL

2005 JavaOneSM Conference | Session TS-7964 | 15


WebMethod

• Specifies that the given method is exposed as a


Web Service operation

• operationName
• Name of the wsdl:operation matching this method
• action
• The action for this operation. For SOAP bindings, this
determines the value of the SOAPAction header

2005 JavaOneSM Conference | Session TS-7964 | 16


Example 1—Hello World

@WebService
public class HelloWorld {

@WebMethod
public String hello(){
return “Hello World!”;
}
}

2005 JavaOneSM Conference | Session TS-7964 | 17


Example 1—Hello World
@WebService
public class HelloWorld {

@WebMethod (operationName=“hi”)
public String hello(){
return “Hello World!”;
}
}

<wsdl:message name="hello">
name=“hi">
<wsdl:part name="parameters" element="tns:helloRequest"/>
element="tns:hiRequest"/>
</wsdl:message>
<wsdl:message name="helloResponse">
name=“hiResponse">
<wsdl:part name="return" element="tns:helloResponse"/>
element="tns:hiResponse"/>
</wsdl:message>

<wsdl:portType name="HelloWorld">
<wsdl:operation name="hello">
name=“hi">
<wsdl:input message="tns:hello"/>
message="tns:hi"/>
<wsdl:output message="tns:helloResponse"/>
message="tns:hiResponse"/>
</wsdl:operation>
</wsdl:portType>
2005 JavaOneSM Conference | Session TS-7964 | 18
Example 1—Hello World

@WebService (serviceName=“myService”)
public class HelloWorld {

@WebMethod
public String hello(){
Default is class return “Hello World!”;
name + “Service” }
}

<wsdl:service name="myService">
name="HelloWorldService">
<wsdl:port name="HelloWorldServicePort“
binding="tns:HelloWorldBinding">
<soap:address location="…"/>
</wsdl:port>
</wsdl:service>

2005 JavaOneSM Conference | Session TS-7964 | 19


Oneway

• Indicates that the given @WebMethod has


only an input message and no output
• Typically returns the thread of control to the calling
application prior to executing the actual business
method
• The method must not have a return value or
Holder parameters, or declare any checked
exceptions

2005 JavaOneSM Conference | Session TS-7964 | 20


SOAPBinding

• Indicates that this Web Service’s methods are to be


mapped to the SOAP message protocol
• Default is DOCUMENT/LITERAL/WRAPPED
• style
• DOCUMENT or RPC
• use
• LITERAL or ENCODED
• parameterStyle
• W RAPPED or BARE

2005 JavaOneSM Conference | Session TS-7964 | 21


WebParam
• Controls the name and namespace of the return value
• name
• For RPC style this is the name of the wsdl:part
• For DOCUMENT style this is the local name of the element
• Default is the parameter name
• targetNamespace
• The XML namespace for the element
• Only used for DOCUMENT style
• Default is the target namespace of the Web Service
• mode
• IN, INOUT, or OUT
• OUT and INOUT parameters must be Holder Types
• OUT and INOUT parameters apply only to RPC style
or parameters that map to headers
• header
2005 JavaOneSM Conference | Session TS-7964 | 22
WebResult

• Provides fine-grained control over the name


and namespace of the return
• Binding determines behavior
• name
• For RPC style this is the name of the wsdl:part
• For DOCUMENT style this is the local name of the element
• Default is “return”
• targetNamespace
• The XML namespace for the element
• Only used for DOCUMENT style
• Default is the target namespace of the W eb Service

2005 JavaOneSM Conference | Session TS-7964 | 23


Example 2—RPC/ENCODED

@WebService
@SOAPBinding(
style=SOAPBinding.Style.RPC,
use=SOAPBinding.Use.ENCODED)
public class EchoService {

@WebMethod(action=“echo”)
@WebResult(name=“echoResult")
public String echo( @WebParam(name=“echoMsg") String msg){
return msg;
}
}

2005 JavaOneSM Conference | Session TS-7964 | 24


Example 2—RPC/ENCODED
@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC,use=SOAPBinding.Use.ENCODED)
public class EchoService {

@WebMethod(action=“echo”)
@WebResult(name=“echoResult")
public String echo( @WebParam(name=“echoMsg") String msg){
return msg;
}
}
<message name="echo">
<part name=“echoMsg" type="s0:string"/>
</message>
<message name="echoResponse">
<part name=“echoResult" type="s0:string"/>
</message>
<portType name="EchoService">
<operation name="echo" parameterOrder=“echoMsg">
<input message="s1:echo"/>
<output message="s1:echoResponse"/>
</operation>
</portType>
2005 JavaOneSM Conference | Session TS-7964 | 25
Example 2—RPC/ENCODED
@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC,use=SOAPBinding.Use.ENCODED)
public class EchoService {

}

<binding name="EchoServiceSoapBinding" type="s1:EchoService">


<s2:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="echo">
<s2:operation soapAction=“echo" style="rpc"/>
<input>
<s2:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://examples/EchoService" use="encoded"/>
</input>
<output>
<s2:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
namespace="http://examples/EchoService" use="encoded"/>
</output>
</operation>
</binding>
2005 JavaOneSM Conference | Session TS-7964 | 26
Example 3—DOCUMENT/LITERAL/BARE

@WebService
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,
use=SOAPBinding.Use.LITERAL,
parameterStyle=SOAPBinding.ParameterStyle.BARE)
public class EchoService {

@WebMethod(action="echo")
@WebResult(targetNamespace=“http://exa.org”, name="echoResult")
public String echo(
@WebParam(targetNamespace=“http://exa.org”, name="echoMsg") String msg){
return msg;
}
}

2005 JavaOneSM Conference | Session TS-7964 | 27


Example 3—DOCUMENT/LITERAL/BARE
@WebService
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,
use=SOAPBinding.Use.LITERAL,
parameterStyle=SOAPBinding.ParameterStyle.BARE)
public class EchoService {

@WebMethod(action="echo")
@WebResult(targetNamespace=“http://exa.org”, name="echoResult")
public String echo(
@WebParam(targetNamespace=“http://exa.org”, name="echoMsg") String msg){
return msg;
}
}
<wsdl:message name="echo">
<wsdl:part name="parameters" element="ns0:echoMsg"/>
<wsdl:types>
</wsdl:message>
<xs:schema targetNamespace=“http://exa.org”
<wsdl:message name="echoResponse">
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<wsdl:part name="echoResult" element="ns0:echoResult"/>
<xs:element name="echoResult" type="xs:string"/>
</wsdl:message>
<xs:element name="EchoService">
<wsdl:portType name="echoMsg" type="xs:string"/>
</xs:schema>
<wsdl:operation name="echo">
<wsdl:input message="tns:echo"/>
</wsdl:types>
<wsdl:output message="tns:echoResponse"/>
</wsdl:operation>
</wsdl:portType> 2005 JavaOne SM
Conference | Session TS-7964 | 28
Example 3—DOCUMENT/LITERAL/BARE
@WebService
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,
use=SOAPBinding.Use.LITERAL,
parameterStyle=SOAPBinding.ParameterStyle.BARE)
public class EchoService {

@WebMethod(action="echo")
@WebResult(targetNamespace=“http://exa.org”, name="echoResult")
public String echo(
@WebParam(targetNamespace=“http://exa.org”, name="echoMsg") String msg){
return msg;
}
}
<wsdl:binding name="EchoServiceBinding" type="tns:EchoService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation
<wsdl:types> name="echo">
<soap:operation soapAction="echo" style="document"/>
<xs:schema targetNamespace=“http://exa.org”
<wsdl:input name="echoInput">
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soap:body parts="parameters" use="literal" namespace="examples"/>
<xs:element name="echoResult" type="xs:string"/>
</wsdl:input>
<xs:element name="echoMsg" type="xs:string"/>
<wsdl:output name="echoOutput">
</xs:schema>
<soap:body parts="echoResult" use="literal" namespace="examples"/>
</wsdl:types>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
2005 JavaOneSM Conference | Session TS-7964 | 29
Example 4—Headers and Mode

@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC,
use=SOAPBinding.Use.LITERAL)
public class EchoHeaderService {

@WebMethod
public String echo(
@WebParam(name="myHeader",
header=true,
mode=WebParam.Mode.INOUT) StringHolder header,
String msg){
String ret = header.value + + ", " + msg;
header.value = "got it";
return ret;
}
} 2005 JavaOneSM Conference | Session TS-7964 | 30
Example 4—Headers and Mode
@WebMethod
public String echo(
@WebParam(name="myHeader",
header=true,
mode=WebParam.Mode.INOUT) StringHolder header,
String msg){
String ret = header.value + ", " + msg;
header.value = "got it";
return ret;
}
<wsdl:binding name="EchoHeaderServiceBinding" type="tns:EchoHeaderService">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="echo">
<soap:operation soapAction="" style="rpc"/>
<wsdl:types>
<wsdl:input name="echoInput">
<xs:schema
<soap:bodytargetNamespace=“http://exa.org”
parts="msg" use="literal" namespace="examples"/>
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<soap:header message="tns:echo" part="myHeader" use="literal"/>
<xs:element name="echoResult" type="xs:string"/>
</wsdl:input>
<xs:element
<wsdl:output name="echoMsg" type="xs:string"/>
name="echoOutput">
</xs:schema>
<soap:body parts="return" use="literal" namespace="examples"/>
</wsdl:types>
<soap:header message="tns:echo" part="myHeader" use="literal"/>
</wsdl:output>
</wsdl:operation>
2005 JavaOne Conference | Session TS-7964
SM
| 31
</wsdl:binding>
Example 4—Headers and Mode
@WebMethod
public String echo(
@WebParam(name="myHeader",
header=true,
mode=WebParam.Mode.INOUT) StringHolder header,
String msg){
String ret = header.value + ", " + msg;
header.value = "got it";
return ret;
}
Request Response
<soap:Envelope xmlns:ns0="examples“ <soap:Envelope xmlns:soap="…">
xmlns:soap="…"> <soap:Header>
<soap:Header> <exam:myHeader xmlns:exam="examples">
<ns0:myHeader>Hello</ns0:myHeader> got it
</soap:Header> </exam:myHeader>
<soap:Body> </soap:Header>
<ns0:echo> <soap:Body>
<msg>How are you?</msg> <exam:echoResponse xmlns:exam="examples">
</ns0:echo> <return>Hello, How are you?</return>
</soap:Body> </exam:echoResponse>
</soap:Envelope> </soap:Body>
</soap:Envelope>
2005 JavaOneSM Conference | Session TS-7964 | 32
HandlerChain

• Associates the Web Service with an externally defined


handler chain
• XML—types are from the http://java.sun.com/xml/ns/j2ee
namespace defined by JSR 109

• file
• Location of the handler chain file
• name
• Name of the handler chain in the file

2005 JavaOneSM Conference | Session TS-7964 | 33


Example Handler Chain File
<handler-config xmlns:j2ee="http://java.sun.com/xml/ns/j2ee">
<handler-chain>
<handler-chain-name>MyHandlerChain</handler-chain-name>
<handler>
<j2ee:handler-name>myHandler</j2ee:handler-name>
<j2ee:handler-class>foo.MyHandler</j2ee:handler-class>
<j2ee:init-param>
<j2ee:param-name>offset</j2ee:param-name>
<j2ee:param-value>1</j2ee:param-value>
</j2ee:init-param>
</handler>
</handler-chain>
</handler-config>

2005 JavaOneSM Conference | Session TS-7964 | 34


SOAPMessageHandler
• Specifies a list of javax.xml.rpc.handler.Handlers to run
before and after the Web Service’s business method
invocation
• name
• Name of the handler
• className
• Fully Qualified name of the handler class
• InitParams
• Array of name/value pairs passed to the handler during initialization
• roles
• List of SOAP roles implemented by the handler
• headers
• List of SOAP headers processed by the handler

2005 JavaOneSM Conference | Session TS-7964 | 35


SOAPMessageHandlers Example

@WebService
@SOAPMessageHandlers({
@SOAPMessageHandler(
name = "myHandler",
className = "foo.myHandler",
initParams = {@InitParam(name="offset", value="1")})
})
public class MyWebService
{
}

2005 JavaOneSM Conference | Session TS-7964 | 36


SOAPMessageHandlers -> HandlerChain File
@WebService
@SOAPMessageHandlers({
@SOAPMessageHandler(
name = "myHandler",
className = "foo.myHandler",
initParams = {@InitParam(name="offset", value="1")})
})
public class MyWebService{…}

<handler-config xmlns:j2ee="http://java.sun.com/xml/ns/j2ee">
<handler-chain>
<handler-chain-name>MyHandlerChain</handler-chain-name>
<handler>
<j2ee:handler-name>myHandler</j2ee:handler-name>
<j2ee:handler-class>foo.MyHandler</j2ee:handler-class>
<j2ee:init-param>
<j2ee:param-name>offset</j2ee:param-name>
<j2ee:param-value>1</j2ee:param-value>
</j2ee:init-param>
</handler>
</handler-chain>
</handler-config>
2005 JavaOneSM Conference | Session TS-7964 | 37
Agenda

• JSR 181
• Development Models
• Processor
• Runtime
• Annotations
• Reference Implementation
• Next Steps
• Summary
• Q&A
2005 JavaOneSM Conference | Session TS-7964 | 38
Reference Implementation

• Demonstrates how 181 annotations affect the Java


technology to WSDL mapping
• Start with Java technology development
mode only
• Command line build processor
• Rudimentary Runtime
• Supports http transport via a Servlet

2005 JavaOneSM Conference | Session TS-7964 | 39


Reference Implementation

compil
produces
JWSC deploy
e

Java Technology Source Java WSC WAR File Web Container

2005 JavaOneSM Conference | Session TS-7964 | 40


Java WSC—The RI compiler

• Command line tool


• Delegates to apt for annotation processing
• Javac for compiling
• Accepts javac and apt arguments
• Produces container agnostic war file

2005 JavaOneSM Conference | Session TS-7964 | 41


Java WSC Usage
Usage: jwsc <jwsc and javac options> <source files>
where jwsc options include:
-classpath <path> Specify where to find user class files
-cp <path> Specify where to find user class files
-d <path> Specify where to place processor and javac generated class files
-s <path> Specify where to place processor generated source files
-source <release> Provide source compatibility with specified release
-version Version information
-help Print a synopsis of standard options; use javac -help for more…
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system
-A[key[=value]] Options to pass to jwsc, [debug]
-nocompile Do not compile source files to class files
-print Print out textual representation of specified types
See javac -help for information on javac options.

2005 JavaOneSM Conference | Session TS-7964 | 42


Java WS—Testing

@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC,
use=SOAPBinding.Use.LITERAL)
public class EchoHeaderService {

@WebMethod
public String echo(
@WebParam(name="myHeader",
header=true,
mode=WebParam.Mode.INOUT) StringHolder header,
String msg){
String ret = header.value + + ", " + msg;
header.value = "got it";
return ret;
}
}

2005 JavaOneSM Conference | Session TS-7964 | 43


Java WS—Testing

2005 JavaOneSM Conference | Session TS-7964 | 44


Agenda

• JSR 181
• Development Models
• Processor
• Runtime
• Annotations
• Reference Implementation
• Next Steps
• Summary
• Q&A
2005 JavaOneSM Conference | Session TS-7964 | 45
Maintenance Release

• Align with Java APIs for XML Web Services


(JAX-WS) 2.0
• Clarify annotation inheritance scenarios
• Annotation for setting name for of element
wrappers for DOCUMENT/LITERAL/WRAPPED
• Allow SOAPBinding annotation per method

2005 JavaOneSM Conference | Session TS-7964 | 46


JSR 181 2.0

• WSDL 2.0
• SOAP 1.2
• Support of JSR 250 Common Annotations
• Improving the quality of service
• Buffering
• Transactional support
• Reliability
• Callbacks
• Conversations
• Policy
• Beyond J2EE platform security
2005 JavaOneSM Conference | Session TS-7964 | 47
Summary

• Rapid Web Service development


• Reduced maintainability (one source of truth)
• Accelerates SOA Implementation

2005 JavaOneSM Conference | Session TS-7964 | 48


For More Information
• JSR 181—Java Web Services Metadata
• http://www.jcp.org/en/jsr/detail?id=181
• Java Web Services Metadata Home—RI download
• http://dev2dev.bea.com/webservices/jwsm.csp
• Java Web Services Metadata in Beehive
• http://incubator.apache.org/beehive

Related Specs
• JSR 101 Java API for XML-based RPC (JAXRPC
• http://jcp.org/en/jsr/detail?id=101
• JSR 109 J2EE™ Platform Web Services
• http://jcp.org/en/jsr/detail?id=109
• JSR 175 Java Language Metadata
• http://jcp.org/en/jsr/detail?id=175
• JSR 224 JAX-WS 2.0
• http://www.jcp.org/en/jsr/detail?id=224
2005 JavaOneSM Conference | Session TS-7964 | 49
Submit Session Evaluations for Prizes!
Your opinions are important to Sun
• You can win a $75.00 gift certificate to the on-site Retail
Store by telling Sun what you think!
• Turn in completed forms to enter the daily drawing
• Each evaluation must be turned in the same day as
the session presentation
• Five winners will be chosen each day (Sun will send
the winners email)
• Drop-off locations: give to the room monitors or use any
of the three drop-off stations in the Northand South Halls
Note: Winners on Thursday, 6/30, will receive and can
redeem certificates via email.
2005 JavaOneSM Conference | Session TS-7964 | 50
Q&A

2005 JavaOneSM Conference | Session TS-7964 | 51


Q&A

2005 JavaOneSM Conference | Session TS-7964 | 52


Java™ Web Services
Development Using
Annotations
Brian Zotter
Staff Engineer
BEA Systems
http://www.bea.com
TS-7964
2005 JavaOne SM Conference | Session TS-7964 53

Vous aimerez peut-être aussi