Project creation
First of all let us create our Web-Application Project. For this, navigate to the directory (or create the directory) where you want to place your project.
Have you done this, just open a console (like the GIT Bash) in this.
For creating the project we will use the following maven archetype.
mvn archetype:generate -Dfilter=com.airhacks:javaee7-essentials-archetype
Just copy this one in your bash and execute it.
You´ll have to set some properties like the groupId or the artifactId. Feel free to name your application however you want.
After the creation you´ll see a folder named with your artifactId.
Let´s start coding
Open the Maven-Project you have just created in the IDE of your choice (for me, it is NetBeans 8.2).
After you opened the project, you may have noticed that there is already a class in your project.
import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; /** * Configures a JAX-RS endpoint. Delete this class, if you are not exposing * JAX-RS resources in your application. * * @author javadevcorner.com */ @ApplicationPath("api") public class JAXRSConfiguration extends Application { }
The JAXRSConfiguration class is the entrance in your application.
You are only allowed to have one(!) JAXRSConfiguration class, per artifact.
Change the value of @ApplicationPath from resource to api just as I did.
And now let´s create the following packages:
- entity
- control
- boundary
Entity Package
package com.javadevcorner.entity; /** * * @author javadevcorner.com */ public class Hero { private int id; private String heroName; private String realName; public Hero() { } //setter and getter }
Control Package
package com.javadevcorner.control; import com.entity.Hero; import java.util.ArrayList; import java.util.List; /** * * @author javadevcorner.com */ public class Avengers { public final static List AVENGERS = new ArrayList<>(); static { Hero captainAmerica = new Hero(); captainAmerica.setId(1); captainAmerica.setHeroName("Captain America"); captainAmerica.setRealName("Steve Rogers"); Hero ironMan = new Hero(); ironMan.setId(2); ironMan.setHeroName("Iron Man"); ironMan.setRealName("Tony Stark"); Hero hulk = new Hero(); hulk.setId(3); hulk.setHeroName("Hulk"); hulk.setRealName("Dr. Bruce Banner"); AVENGERS.add(captainAmerica); AVENGERS.add(ironMan); AVENGERS.add(hulk); } }
Boundary Package
First of all move the JAXRSConfiguration class to the boundary section. Just for clean code and overview, not technical background.
And then let´s implement our JAX-RS Get Method. Create the class AvengersService and implement as followed.
package com.javadevcorner.boundary; import com.control.Avengers; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; /** * * @author javadevcorner.com */ @Path("callAvengers") public class AvengersService { @GET @Path("allAvengers") @Produces(MediaType.APPLICATION_JSON) public Response avengersWeNeedYou() { return Response .status(Response.Status.OK) .entity(Avengers.AVENGERS) .build(); } }
Let´s talk about what our implementation is doing
- @Path as Class Annotation: Just specifies the path on which the HTTP-Request can find our Avengers
- @GET on our method: Specifies that this Method is a HTTP-GET Method
- @Produces on our method: Defines which sort of Response the Request can expect
But enough theory for now, let´s test our implementation.
Start your Wildfly installation and make sure it is running. If you haven´t have one please check out this tutorial.
To deploy our Application to Wildfly I recommend to you the Wildfly Maven Plugin. When you have added this Plugin to your project, navigate with a console in the project directory, in which you have your pom.xml and type:
mvn wildfly:deploy
The console will now print some output. After the build finished with success, open your browser and open your Rest-Application (for the pretty view of my JSON I installed a Google Chrome Plugin)
So thanks for reading and have fun using JAX-RS.
Leave a Reply