Wednesday, June 5, 2013

Restful project with Maven Archetype and Jersey

Hi everyone, this is my first post and hope you like it.

Today we will talk about building a Restful project using maven and jersey libraries. For this to be possible maven archetype, a tool to build projects out of templates, will be used.

In this tutorial we will be using command line maven, you can download and follow the installation intructions on http://maven.apache.org/download.cgi.


After installation follow the steps below:

1 - Go to your workspace and type:

mvn archetype:generate -DarchetypeCatalog=http://download.java.net/maven/2

2 - Archetypes will be downloaded from the catalog. After downloading you'll see this message "Choose a number or apply a filter", type 3.

3 - After selecting rest archetype, you will fill the project configuration information according to the example below:
groupId - br.com.restful (Identifies your project uniquely between all your projects)
artifactId - blog-restful-webapp (Represents the war name without version)
version - 1.0 (Identifies the project version)
package - br.com.restful (Represent the package structure)

4 - Next, a confirmation message will show up, just type Y and your project will be created.

5 - After the project have been created, type: 

cd PROJECT_NAME
mvn clean install (Clean and build)

6 - The result of previous step should be BUILD SUCCESS, which means that now you already have your rest project fully configured, compiling and ready to be executed.

After doing all these steps you will have import the project into your IDE. In this example eclipse will take place, but be free to use your prefered IDE since maven is IDE agnostic.
Open eclipse and click with the mouse right button on Project Explorer -> Import -> Existing Maven Projects, select your project and click Finish.


 

Now you can take a look on the class created by the template in br.com.restful.MyResource.




Next, click on the mouse right button over the project and run on your server. In this example we used Tomcat 6.


A page like this should appear.


To access the GET method you just have to access the link:
http://localhost:8080/blog-restful-webapp/webresources/myresource, remember that port should be set accordingly to the server configuration. On this case we used tomcat default port which is 8080.



The template initial configuration establishes that all rest services stays below webresources. This could be easily changed but this is a subject for other post.


Hope you enjoyed it and see you later.

Source:
http://docs.oracle.com/cd/E19226-01/820-7627/giqdq/

12 comments:

  1. Pra ganhar um 10 com estrelinha, faltou colocar um mvn eclipse:eclipse.
    ;)

    Parabéns Ivan pela iniciativa.

    ReplyDelete
  2. Ola Ivan parabens pelo post me ajudou muito ! Voce teria algum exemplo utilizando json?

    ReplyDelete
  3. Obrigado pelo feedback Rodrigo. No meu github https://github.com/ivanjunckes/rest-client-api-example tem um exemplo de como fazer com JSON. Basicamente vocĂȘ vai ter que mudar o valor dentro de @Produces para application/json.

    Abs

    ReplyDelete

  4. Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.
    Best AWS Training in Chennai | Amazon Web Services Training Institute in Chennai Velachery, Tambaram, OMR
    Advanced AWS Training in Bangalore |Best AWS Training Institute in Bangalore BTMLA ,Marathahalli

    ReplyDelete
  5. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
    microsoft azure training in bangalore
    rpa interview questions and answers
    automation anywhere interview questions and answers
    blueprism interview questions and answers
    uipath interview questions and answers
    rpa training in bangalore

    ReplyDelete
  6. I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ReplyDelete