Search This Blog

Tuesday, February 24, 2015

SOAP Webservice using CXF and Maven

In this post, we will see how to create a JAX-WS SOAP based 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=testjaxrsws -DarchetypeArtifactId=org.apache.cxf.archetype:cxf-jaxws-javafirst -DinteractiveMode=false


Note:
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-jaxws-javafirst".

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


3.

i. Test using browser


Open the browser and navigate to the below URL :

http://<host>:<port>/jaxws-service/helloWorld/echo/learninjava

ii. Test using client


Run the below command to run the client :

mvn test

Note:
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-WS SOAP webservice, refer
http://www.learninjava.com/pages/jaxws-soap-webservice-using-cxf.php

Friday, January 30, 2015

JAX-RS Annotations

@Path - at class level specifies the context path to access this web service
 

@Path - at method level specifies the path to access the actual method in the web service
 

@Produces - is used to specify the MIME media type for the return type of the method
 

@Consumes - is used to specify the MIME media type for the parameter type accepted as input to a method
 

@PathParam - is used to accept input from the request URL path specified by @Path
 

@QueryParam - is used to get query parameters from URL
 

@DefaultValue - is used to specify default value if no query parameters are specified
 

@MatrixParam - is used to specify a set of "name=value" pairs separated by ";" in the URI path
 

@CookieParam - is used to get the parameters from cookie, eg, 
@CookieParam(value="User-Agent")
 

@FormParam - is used to get HTML form parameter values
 

@Context - is used to get the contextual java types of request or responses like ServletContext, ServletConfig,
    HttpServletRequest and HttpServletResponse etc
 

@BeanParam - is used to hold multiple parameters as a single entity (bean)
 

@HeaderParam - is used to get information from the HTTP headers

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



Note:
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


3. 

 

i. Test using browser 


Open the browser and navigate to the below URL :

http://<host>:<port>/jaxrs-service/helloWorld/echo/learninjava


ii. Test using client


Run the below command to run the client :

mvn test

Note:
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
http://www.learninjava.com/pages/restful-webservice-using-cxf.php