2. Using the Tutorial Examples 3. Getting Started with Web Applications 5. JavaServer Pages Technology 7. JavaServer Pages Standard Tag Library 10. JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages 12. Developing with JavaServer Faces Technology 13. Creating Custom UI Components 14. Configuring JavaServer Faces Applications 15. Internationalizing and Localizing Web Applications 16. Building Web Services with JAX-WS Creating a Simple Web Service and Client with JAX-WS Requirements of a JAX-WS Endpoint Coding the Service Endpoint Implementation Class Building, Packaging, and Deploying the Service Building, Packaging, and Deploying the Service Using NetBeans IDE Building, Packaging, and Deploying the Service Using Ant Web Services Interoperability and JAX-WS Further Information about JAX-WS 17. Binding between XML Schema and Java Classes 19. SOAP with Attachments API for Java 21. Getting Started with Enterprise Beans 23. A Message-Driven Bean Example 24. Introduction to the Java Persistence API 25. Persistence in the Web Tier 26. Persistence in the EJB Tier 27. The Java Persistence Query Language 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API 36. The Coffee Break Application | Creating a Simple Web Service and Client with JAX-WSThis section shows how to build and deploy a simple web service andclient. The source code for the service is intut-install/javaeetutorial5/examples/jaxws/helloservice/ and theclient is intut-install/javaeetutorial5/examples/jaxws/simpleclient/. Figure 16-1 illustrates how JAX-WS technology manages communication between a web service and client. Figure 16-1 Communication between a JAX-WS Web Service and a Client ![]() The starting point for developing a JAX-WS web service is a Javaclass annotated with thejavax.jws.WebService annotation. The@WebService annotation defines the class as aweb service endpoint. Aservice endpoint interface orservice endpoint implementation (SEI) is a Java interface or class, respectively, thatdeclares the methods that a client can invoke on the service. Aninterface is not required when building a JAX-WS endpoint. The web service implementation classimplicitly defines an SEI. You may specify an explicit interface by adding theendpointInterface element tothe@WebService annotation in the implementation class. You must then provide an interfacethat defines the public methods made available in the endpoint implementation class. You use the endpoint implementation class and thewsgen tool to generate the webservice artifacts that connect a web service client to the JAX-WS runtime. Together, thewsgen tool and the Application Server provide the Application Server’s implementationof JAX-WS. These are the basic steps for creating the web service and client:
The sections that follow cover these steps in greater detail. Requirements of a JAX-WS EndpointJAX-WS endpoints must follow these requirements:
Coding the Service Endpoint Implementation ClassIn this example, the implementation class,Hello, is annotated as a web serviceendpoint using the@WebService annotation.Hello declares a single method namedsayHello, annotatedwith the@WebMethod annotation.@WebMethod exposes the annotated method to web serviceclients.sayHello returns a greeting to the client, using the name passedtosayHello to compose the greeting. The implementation class also must define adefault, public, no-argument constructor. package helloservice.endpoint;import javax.jws.WebService;@WebServicepublic class Hello { private String message = new String("Hello, "); public void Hello() {} @WebMethod public String sayHello(String name) { return message + name + "."; }}Building, Packaging, and Deploying the ServiceYou can build, package, and deploy thehelloservice application using either NetBeans IDEorant. Building, Packaging, and Deploying the Service Using NetBeans IDEFollow these instructions to build, package, and deploy thehelloservice example toyour Application Server instance using NetBeans IDE.
This builds and packages to application intohelloservice.war, located intut-install/javaeetutorial5/examples/jaxws/helloservice/dist/, and deploysthis WAR file to your Application Server instance. Building, Packaging, and Deploying the Service Using AntTo build and packagehelloservice using Ant, in a terminal window, go tothetut-install/javaeetutorial5/examples/jaxws/helloservice/ directory and type the following: ant This command calls thedefault target, which builds and packages the application intoan WAR file,helloservice.war, located in thedist directory. To deploy thehelloservice example, follow these steps:
You can view the WSDL file of the deployed service by requestingthe URLhttp://localhost:8080/helloservice/hello?WSDL in a web browser. Now you are ready to create aclient that accesses this service. Undeploying the ServiceAt this point in the tutorial, do not undeploy the service. Whenyou are finished with this example, you can undeploy the service by typingthis command: ant undeploy Theall TaskAs a convenience, theall task will build, package, and deploy the application.To do this, enter the following command: ant all Testing the Service without a ClientThe Application Server Admin Console allows you to test the methods ofa web service endpoint. To test thesayHello method ofHelloService, do thefollowing:
A Simple JAX-WS ClientHelloClient is a stand-alone Java program that accesses thesayHello method ofHelloService. Itmakes this call through a port, a local object that acts asa proxy for the remote service. The port is created at development timeby thewsimport tool, which generates JAX-WS portable artifacts based on aWSDL file. Coding the ClientWhen invoking the remote methods on the port, the client performs these steps:
Here is the full source ofHelloClient, which is located in thetut-install/javaeetutorial5/examples/jaxws/simpleclient/src/java/directory. package simpleclient;import javax.xml.ws.WebServiceRef;import helloservice.endpoint.HelloService;import helloservice.endpoint.Hello;public class HelloClient { @WebServiceRef(wsdlLocation="http://localhost:8080/ helloservice/hello?wsdl") static HelloService service; public static void main(String[] args) { try { HelloClient client = new HelloClient(); client.doTest(args); } catch(Exception e) { e.printStackTrace(); } } public void doTest(String[] args) { try { System.out.println("Retrieving the port from the following service: " + service); Hello port = service.getHelloPort(); System.out.println("Invoking the sayHello operation on the port."); String name; if (args.length > 0) { name = args[0]; } else { name = "No Name"; } String response = port.sayHello(name); System.out.println(response); } catch(Exception e) { e.printStackTrace(); } }}Building and Running the ClientYou can build and run thesimpleclient application using either NetBeans IDE orant. To build the client, you must first have deployedhelloservice, as described inBuilding, Packaging, and Deploying the Service. Building and Running the Client in NetBeans IDEDo the following to build and runsimpleclient:
You will see the output of the application client in the Outputpane. Building and Running the Client Using AntIn a terminal navigate totut-install/examples/jaxws/simpleclient/ and type the following command: ant This command calls thedefault target, which builds and packages the application intoa JAR file,simpleclient.jar, located in thedist directory. The run the client, type the following command: ant run Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |