Search This Blog

Saturday, January 17, 2015

Restful Webservice using CXF and Maven

In this post, we will see how to create a webservice and a client using Apache CXF and Maven. Infact, this can be done without writing a single line of code.

Find below the step by step process to create one:

1. Create the project structure and sample files using the below command:

mvn archetype:generate -DgroupId=com.learninjava -DartifactId=cxfjaxrsws    -DarchetypeArtifactId=org.apache.cxf.archetype:cxf-jaxrs-service -DinteractiveMode=false

If you are seeing weird errors when you run the above command then you can use the below command :

mvn archetype:generate

then maven will list all the archetypes available, select the number referring to the archetype "org.apache.cxf.archetype:cxf-jaxrs-service".

If you are unable to see the entire list, then write the list to a file and find the appropriate archetype in the persisted file.

Command to write the archetypes list to a file :

mvn archetype:generate > archetypes.txt 

2. Start the server and webservice

The above command will create basic project structure and all the required files and configuration. Open pom.xml and you can see the tomcat plugin is also added. We will use this plugin for our server. Use the below command to start tomcat :

mvn clean install -DskipTests=true tomcat7:run



i. Test using browser 

Open the browser and navigate to the below URL :


ii. Test using client

Run the below command to run the client :

mvn test

1. Make sure the server is running before the client is started
2. Make sure the test file name has the name "Test" in it, otherwise maven
simply ignores the file thinking of it as non test source file.

For detailed information on creating a JAX-RS Restful webservice, refer

No comments: