JAX-WS
Another Java
Web Services Framework
Overview

Included in Java 6 Standard Edition

Available as download for Java 5

Annotation Based

Tools available for...
WAR Structure
my.war
index.jsp
META-INF
MANIFEST.MF
WEB-INF
classes
com/mypackage/MyClass.class
lib
some.jar
web.xml
sun-j...
Web App Config
WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xm...
Web Service Config
1
2
3
4
5
6
7
WEB-INF/sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://jav...
Minimal Service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.blogspot.weswilliams.hello.ws;
import java.uti...
Minimal SOAP WSDL
<?xml version='1.0' encoding='UTF-8'?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
x...
Minimal SOAP XSD
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:tns="http://ws.hello.weswilliams.blogspot.com/"
x...
Minimal SOAP Message
Request:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ws="http:/...
Customized Service
Hello.java:
package com.blogspot.weswilliams.hello2.ws;
import javax.jws.*;
@WebService(targetNamespace...
Customized Implementation
HelloImpl.java:
package com.blogspot.weswilliams.hello2.ws;
import javax.jws.WebService;
import ...
Configuration Difference
WEB-INF/sun-jaxws.xml
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.co...
Customized WSDL
<?xml version='1.0' encoding='UTF-8'?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xml...
Customized XSD
<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:tns="http://wes-williams.blogspot.com/Hi"
xmlns:xs=...
Provide a Custom WSDL
Hello.java:
package com.blogspot.weswilliams.hello2.ws;
import javax.jws.*;
@WebService(name = "HiSe...
Custom WSDL
<?xml version='1.0' encoding='UTF-8'?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:t...
Customized Messages
Request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:hi="http://...
Client Generation
Generation:
wsimport -keep -p <PACKAGE_FOR_CLIENT> -d <GENERATION_DIRECTORY> <WSDL_URL>
Output:
parsing ...
More JAX-WS Resources
General:
https://jax-ws.dev.java.net/
http://java.sun.com/developer/technicalArticles/J2SE/jax_ws_2/...
Upcoming SlideShare
Loading in...5
×

JAX-WS Basics

3,043

Published on

JAX-WS is a simple way to create web services on the Java Platform

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,043
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
367
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

JAX-WS Basics

  1. 1. JAX-WS Another Java Web Services Framework
  2. 2. Overview  Included in Java 6 Standard Edition  Available as download for Java 5  Annotation Based  Tools available for WSDL consumption  Development Approaches  Contract First (Top Down)  Code First (Bottom Up)
  3. 3. WAR Structure my.war index.jsp META-INF MANIFEST.MF WEB-INF classes com/mypackage/MyClass.class lib some.jar web.xml sun-jaxws.xml
  4. 4. Web App Config WEB-INF/web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <listener> <listenerclass> com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener> <servlet> <servlet-name>WSServlet</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>WSServlet</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
  5. 5. Web Service Config 1 2 3 4 5 6 7 WEB-INF/sun-jaxws.xml <?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'> <endpoint name="Hello" implementation="com.blogspot.weswilliams.hello.ws.Hello" url-pattern="/Hello" /> </endpoints>
  6. 6. Minimal Service 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 package com.blogspot.weswilliams.hello.ws; import java.util.Date; import javax.jws.*; @WebService public class Hello { public Hello() { System.out.println(new Date() + " - Ready to Say Hello"); } public String sayHello(String name) { System.out.println(new Date() + " - saying hello to " + name); return "Hello " + name + "!"; } }
  7. 7. Minimal SOAP WSDL <?xml version='1.0' encoding='UTF-8'?> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.hello.weswilliams.blogspot.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.hello.weswilliams.blogspot.com/" name="HelloService"> <types> <xsd:schema> <xsd:import namespace="http://ws.hello.weswilliams.blogspot.com/" schemaLocation="http://localhost:8080/hello/Hello?xsd=1" /> </xsd:schema> </types> <message name="sayHello"> <part name="parameters" element="tns:sayHello" /> </message> <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse" /> </message> <portType name="Hello"> <operation name="sayHello"> <input message="tns:sayHello" /> <output message="tns:sayHelloResponse" /> </operation> </portType> <binding name="HelloPortBinding" type="tns:Hello"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> <operation name="sayHello"> <soap:operation soapAction="" /> <input> <soap:body use="literal" /> </input> <output> <soap:body use="literal" /> </output> </operation> </binding> <service name="HelloService"> <port name="HelloPort" binding="tns:HelloPortBinding"> <soap:address location="http://localhost:8080/hello/Hello" /> </port> </service> </definitions>
  8. 8. Minimal SOAP XSD <?xml version='1.0' encoding='UTF-8'?> <xs:schema xmlns:tns="http://ws.hello.weswilliams.blogspot.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://ws.hello.weswilliams.blogspot.com/"> <xs:element name="sayHello" type="tns:sayHello" /> <xs:element name="sayHelloResponse" type="tns:sayHelloResponse" /> <xs:complexType name="sayHello"> <xs:sequence> <xs:element name="arg0" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> <xs:complexType name="sayHelloResponse"> <xs:sequence> <xs:element name="return" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:schema> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  9. 9. Minimal SOAP Message Request: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.hello.weswilliams.blogspot.com/"> <soapenv:Header/> <soapenv:Body> <ws:sayHello> <!--Optional:--> <arg0>Wes</arg0> </ws:sayHello> </soapenv:Body> </soapenv:Envelope> Response: <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:sayHelloResponse xmlns:ns2="http://ws.hello.weswilliams.blogspot.com/"> <return>Hello Wes!</return> </ns2:sayHelloResponse> </S:Body> </S:Envelope> 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8
  10. 10. Customized Service Hello.java: package com.blogspot.weswilliams.hello2.ws; import javax.jws.*; @WebService(targetNamespace = "http://wes-williams.blogspot.com/Hi") public interface Hello { @WebResult(name = "sayHiMessage") @WebMethod(operationName = "sayHi") public String sayHello(@WebParam(name = "name") String name); } 1 2 3 4 5 6 7 8 9 10
  11. 11. Customized Implementation HelloImpl.java: package com.blogspot.weswilliams.hello2.ws; import javax.jws.WebService; import java.util.Date; @WebService(name = "HiService", targetNamespace = "http://wes-williams.blogspot.com/Hi", serviceName = "HiService", endpointInterface = "com.blogspot.weswilliams.hello2.ws.Hello") public class HelloImpl implements Hello { public HelloImpl() { System.out.println(new Date() + " - Ready to Say Hi"); } public String sayHello(String name) { System.out.println(new Date() + " - saying hello to " + name); return "Hello " + name + "!"; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  12. 12. Configuration Difference WEB-INF/sun-jaxws.xml <?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'> <endpoint name="Hello" implementation="com.blogspot.weswilliams.hello2.ws.HelloImpl" url-pattern="/Hello" /> </endpoints> 1 2 3 4 5 6 7
  13. 13. Customized WSDL <?xml version='1.0' encoding='UTF-8'?> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://wes-williams.blogspot.com/Hi" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://wes-williams.blogspot.com/Hi" name="HiService"> <types> <xsd:schema> <xsd:import namespace="http://wes-williams.blogspot.com/Hi" schemaLocation="http://localhost:8080/hello/Hello?xsd=1" /> </xsd:schema> </types> <message name="sayHi"> <part name="parameters" element="tns:sayHi" /> </message> <message name="sayHiResponse"> <part name="parameters" element="tns:sayHiResponse" /> </message> <portType name="HiService"> <operation name="sayHi"> <input message="tns:sayHi" /> <output message="tns:sayHiResponse" /> </operation> </portType> <binding name="HiServicePortBinding" type="tns:HiService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> <operation name="sayHi"> <soap:operation soapAction="" /> <input> <soap:body use="literal" /> </input> <output> <soap:body use="literal" /> </output> </operation> </binding> <service name="HiService"> <port name="HiServicePort" binding="tns:HiServicePortBinding"> <soap:address location="http://localhost:8080/hello/Hello" /> </port> </service> </definitions>
  14. 14. Customized XSD <?xml version='1.0' encoding='UTF-8'?> <xs:schema xmlns:tns="http://wes-williams.blogspot.com/Hi" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://wes-williams.blogspot.com/Hi"> <xs:element name="sayHi" type="tns:sayHi" /> <xs:element name="sayHiResponse" type="tns:sayHiResponse" /> <xs:complexType name="sayHi"> <xs:sequence> <xs:element name="name" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> <xs:complexType name="sayHiResponse"> <xs:sequence> <xs:element name="sayHiResult" type="xs:string" minOccurs="0" /> </xs:sequence> </xs:complexType> </xs:schema> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  15. 15. Provide a Custom WSDL Hello.java: package com.blogspot.weswilliams.hello2.ws; import javax.jws.*; @WebService(name = "HiService", targetNamespace = "http://wes-williams.blogspot.com/Hi", wsdlLocation="WEB-INF/wsdl/HiService.wsdl") public interface Hello { @WebResult(name = "sayHiMessage") @WebMethod(operationName = "sayHi") public String sayHello(@WebParam(name = "name") String name); } 1 2 3 4 5 6 7 8 9 10 11 12 13
  16. 16. Custom WSDL <?xml version='1.0' encoding='UTF-8'?> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://wes-williams.blogspot.com/Hi" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://wes-williams.blogspot.com/Hi" name="HiService"> <types> <xs:schema xmlns:tns="http://wes-williams.blogspot.com/Hi" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://wes-williams.blogspot.com/Hi"> <xs:element name="sayHi" type="tns:sayHi" /> <xs:element name="sayHiResponse" type="tns:sayHiResponse" /> <xs:complexType name="sayHi"> <xs:sequence> <xs:element name="name" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="sayHiResponse"> <xs:sequence> <xs:element name="sayHiMessage" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:schema> </types> . . . . . .
  17. 17. Customized Messages Request: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hi="http://wes-williams.blogspot.com/Hi"> <soapenv:Header/> <soapenv:Body> <hi:sayHi> <name>Wes</name> </hi:sayHi> </soapenv:Body> </soapenv:Envelope> Response: <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> <S:Body> <ns2:sayHiResponse xmlns:ns2="http://wes-williams.blogspot.com/Hi"> <sayHiMessage>Hi Wes!</sayHiMessage> </ns2:sayHiResponse> </S:Body> </S:Envelope> 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7
  18. 18. Client Generation Generation: wsimport -keep -p <PACKAGE_FOR_CLIENT> -d <GENERATION_DIRECTORY> <WSDL_URL> Output: parsing WSDL... generating code... compiling code... Usage: import com.blogspot.weswilliams.hello2.ws.client.*; public class Test { public static void main(String[] args) { HiService hiClient = new HiService_Service().getHiServicePort(); System.out.println(hiClient.sayHi("Wes")); } } 1 2 3 4 5 6 7 8 9 10
  19. 19. More JAX-WS Resources General: https://jax-ws.dev.java.net/ http://java.sun.com/developer/technicalArticles/J2SE/jax_ws_2/ Apache CFX: http://cfx.apache.org Related Resources: http://soapui.org Book Recommendation: O'Reily's Java Web Services: Up and Running wes-williams.blogspot.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×