This sample provides an example of service development using a code first approach using
the JAX-WS APIs. The HelloWorld.java
file included with this sample defines
the service, and the Client.java
file defines a Client
class with
a Client()
method that calls different users.
These instructions use Maven to build and run the sample, using the pom.xml
file located the base directory of this sample. If you prefer to use wsdl2java, javac, and
java to build and run the sample, see the ReadMe file located in your
install_dir\samples\java_first_jaxws
directory.
See About Maven and Installing and setting up Maven for more information about using Maven with the FUSE Services Framework samples.
To build and run the sample using Maven commands:
Open a console to the sample directory, install_dir\samples\java_first_jaxws
and enter the following
command to build the sample:
mvn install
Maven builds the sample and downloads required JAR files to your Maven repository:
[INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building Unnamed - org.apache.cxf.samples:java_first_jaxws:jar:1.0 [INFO] task-segment: [install] [INFO] ------------------------------------------------------------------------ ... [INFO] Installing install_dir\samples\java_first_jaxws\target\java_first_jaxws-1.0.jar to ...\.m2\repository\org\apache\cxf\samples\java_first_jaxws\1.0\java_first_jaxws-1.0.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ ...
Enter the following command to start the server:
mvn -Pserver
When the server is started, the console displays the message Server
ready...
.
Open another console at the same location and enter the following command to start the client:
mvn -Pclient
The client starts and sends a greeting to the server. Messages in the client and server consoles show the messages sent as the sample runs.
The messages between the client and server appear rapidly in the consoles as messages are exchanged. When the messages have all been sent, you can scroll up in the console windows to track the following events:
The client creates the HellpWorld
service, and receives responses
from the server:
Client Console
INFO: Creating Service {http://server.hw.demo/}
HelloWorld from class demo.hw.server.HelloWorld
Hello World
Messages in the server console indicate that sayHi
,
sayHiToUser
, and getUsers
are called:
Server Console
sayHi called
sayHiToUser called
sayHiToUser called
sayHiToUser called
getUsers called
The client receives responses to the sayHiToUser
from the server, and
prints a list of users from the getUsers
call:
Client Console
Hello World
Hello Galaxy
Hello Universe
Users:
1: World
2: Galaxy
3: Universe
To remove the code generated from the WSDL file and the *.class
files, run mvn clean
.
When you run the Maven mvn install
command, Maven compiles the Java files
and creates Java class files. Maven creates the
install_dir\samples\java_first_jaxws\target
directory, which
includes the client and server class files.
When you run the Maven mvn -Pserver
and mvn -Pclient
commands,
Maven starts the server and client and executes the operations in the Java class
files.
The sample files in the wsdl_first
directory include the
following:
pom.xml
— This file is used by the Maven tooling
when creating the service unit and required files for packaging and deploying the
service into a container.
install_dir\samples\java_first_jaxws\src\demo\hw\server\hello_world.java
— This file defines the Hello World web service in this sample:
Example 5.4. Java-First Sample: hello_world.java
@WebService
public interface HelloWorld {
String sayHi(String text);
/* Advanced usecase of passing an Interface in. JAX-WS/JAXB does not
* support interfaces directly. Special XmlAdapter classes need to
* be written to handle them
*/
String sayHiToUser(User user);
/* Map passing
* JAXB also does not support Maps. It handles Lists great, but Maps are
* not supported directly. They also require use of a XmlAdapter to map
* the maps into beans that JAXB can use.
*/
@XmlJavaTypeAdapter(IntegerUserMapAdapter.class)
Map<Integer, User> getUsers();
}
install_dir\samples\java_first_jaxws\src\demo\hw\server\*.java
— Additional Java files that define classes used by the server when this
sample runs.
install_dir\samples\java_first_jaxws\src\demo\hw\client\client.java
— Java file that defines the client class.
The Client.java
file defines the Client()
method:
Example 5.5. Java-First Sample: client.java
... private Client() { } public static void main(String args[]) throws Exception { Service service = Service.create(SERVICE_NAME); // Endpoint Address String endpointAddress = "http://localhost:9000/helloWorld"; // Add a port to the Service service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress); HelloWorld hw = service.getPort(HelloWorld.class); System.out.println(hw.sayHi("World")); User user = new UserImpl("World"); System.out.println(hw.sayHiToUser(user)); //say hi to some more users to fill up the map a bit user = new UserImpl("Galaxy"); System.out.println(hw.sayHiToUser(user)); user = new UserImpl("Universe"); System.out.println(hw.sayHiToUser(user)); System.out.println(); System.out.println("Users: "); Map<Integer, User> users = hw.getUsers(); for (Map.Entry<Integer, User> e : users.entrySet()) { System.out.println(" " + e.getKey() + ": " + e.getValue().getName()); } } ...
The Client()
method in this Java file calls the Java classes defined
on the server to get the users World
, Galaxy
, and
Universe
, and to map the users and print out a list of users. Examine
the client.java
file and the Java files for the server to see
how this Java-first sample is coded and implemented.