This example will lead you through creating your first service with doing "code first" development with JAX-WS. You'll learn how to:
- Set up your build for CXF
- Writing a simple JAX-WS service
- Publish your service using the JAX-WS APIs
This example corresponds to the hello_world_code_first example in the CXF distribution. IMPORTANT: This sample is only in CXF 2.0.1+!
Setting up your build
Open up your favorite IDE and create a new project. The first thing we need to do is add the necessary CXF dependencies to the project. You can find these dependencies in the CXF distribution in the lib directory.
Code Block |
---|
commons-logging-1.1.jar
geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)
geronimo-annotation_1.0_spec-1.1.jar (JSR 250)
geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)
geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)
geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.0.jar
jaxb-impl-2.0.5.jar
jaxws-api-2.0.jar
neethi-2.0.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
stax-api-1.0.1.jar
wsdl4j-1.6.1.jar
wstx-asl-3.2.1.jar
XmlSchema-1.2.jar
xml-resolver-1.2.jar
|
The Spring jars (optional - for XML Configuration support):
Code Block |
---|
aopalliance-1.0.jar
spring-core-2.0.4.jar
spring-beans-2.0.4.jar
spring-context-2.0.4.jar
spring-web-2.0.4.jar
|
And the CXF jar:
Code Block |
---|
cxf-2.0-incubator.jar
|
Writing your Service
First we'll write our service interface. It will have one operation called "sayHello" which says "Hello" to whoever submits their name.
Wiki Markup |
---|
{snippet:id=service|lang=java|url=cxf/trunk/distribution/src/main/release/samples/hello_world_code_first/src/demo/hw/server/HelloWorld.java?revision=HEAD} |
Our implementation will then look like this:
Wiki Markup |
---|
{snippet:id=service|lang=java|url=cxf/trunk/distribution/src/main/release/samples/hello_world_code_first/src/demo/hw/server/HelloWorldImpl.java?revision=HEAD} |
The @WebService annotation on the implementation class lets CXF know which interface we want to create our WSDL with. In this case its simply our HelloWorld interface.
Publishing your service
Wiki Markup |
---|
{snippet:id=publish|lang=java|url=cxf/trunk/distribution/src/main/release/samples/hello_world_code_first/src/demo/hw/server/Server.java?revision=HEAD} |
...