This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
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™ technologybased 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
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
Hello World
Exposes this class as a Web Service
@WebService public class HelloWorld { @WebMethod public String hello(){ return “Hello World!”; }
Exposes this method as a Web Service operation
}
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 (JWS) Source File
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 name="helloResponse"> name=“hiResponse"> <wsdl:part name="return" element="tns:helloResponse"/> element="tns:hiResponse"/> <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"/> 2005 JavaOneSM Conference | Session TS-7964 |
18
Example 1—Hello World @WebService (serviceName=“myService”) public class HelloWorld { @WebMethod public String hello(){ return “Hello World!”; }
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
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 name="echoResponse"> <part name=“echoResult" type="s0:string"/> <portType name="EchoService"> 2005 JavaOneSM Conference | Session TS-7964 |
25
Example 2—RPC/ENCODED @WebService @SOAPBinding(style=SOAPBinding.Style.RPC,use=SOAPBinding.Use.ENCODED) public class EchoService { … }
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 <soap:Envelope xmlns:ns0="examples“ xmlns:soap="…"> <soap:Header> <ns0:myHeader>Hello <soap:Body> <ns0:echo> <msg>How are you?
Response <soap:Envelope xmlns:soap="…"> <soap:Header> <exam:myHeader xmlns:exam="examples"> got it <soap:Body> <exam:echoResponse xmlns:exam="examples"> Hello, How are you? 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 MyHandlerChain <j2ee:handler-name>myHandler <j2ee:handler-class>foo.MyHandler <j2ee:init-param> <j2ee:param-name>offset <j2ee:param-value>1
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{…} MyHandlerChain <j2ee:handler-name>myHandler <j2ee:handler-class>foo.MyHandler <j2ee:init-param> <j2ee:param-name>offset <j2ee:param-value>1
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 e Java Technology Source
produces JWSC Java WSC
deploy 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 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 Pass 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; } }
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 • • • • • • •
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