How to Create a Web Service Using JBoss and JAX-RS
Web Services give us access to extra functionality without writing more code.
You’re the head of development at a new credit card company and your team has built a great product.
Well, almost.You can't send anyone a new card until you verify their credit history. And if you build that functionality yourself, it will slow down development.
So what do you do?
You borrow the functionality from a company that has already figured this out.
And to do that, you use a web service.
Why Do We Use Web Services?
Web services help us build software quickly because they give us access to extra functionality without writing more code.
A web service is a method that our application can use over the internet.
How Do We Use Web Services?
Our Final Architecture
What You’ll Need
- A basic understanding of Maven (read this)
- JBoss Developer Studio w/ JBoss EAP 6.4+ Download here
- JBoss Maven Repositories (Section 1: Steps 4-7)
Create Data Model
Step 1: Open JBoss Developer Studio
Step 2: Create a new Maven project called simple-data-model (Steps here)
Step 3: Create a class called CreditReport
- Make it serializable
public class CreditReport implements Serializable
- Add a default serial version id
private static final long serialVersionUID = 1L;
- Add a constructor
public CreditReport(){
}
Step 4: Create fields to store credit report data
- Create three variables
private String fullname;
private String ssn;
private Integer creditScore;
- Create getters & setters for each field
- Click Source -> Generate Getters & Setters
Step 5: Build the project
- Right click project -> Run as -> Maven Build
Goals: clean install
Create a REST Web Service
Step 1: Create a new maven project called simple-rest-service
Step 2: Update Maven
- In your pom.xml file, click the pom.xml tab and change the packaging type <packaging>war</packaging>
- Add dependencies: resteasy-jaxrs (provided), simple-data-model
- Right click your project -> Maven -> Update Project
Step 3: Add web descriptors
- In the /src/main/webapp/WEB-INF directory, create a file called jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>simple-ejb-project</context-root>
</jboss-web>
- In the same directory, create a file called web.xml
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Step 4: Create a class called CreditReportService
- Add @Path annotation
@Path("/creditReport")
public class CreditReportService
- Add a method called getCreditReport
@GET
@Path("/get")
@Produces("application/json")
public CreditReport getCreditReport(
@QueryParam("fullname") String fullname,
@QueryParam("ssn") String ssn){
CreditReport creditReport = new CreditReport();
creditReport.setFullname(fullname);
creditReport.setSsn(ssn);
Random rand = new Random();
int randomCreditScore = rand.nextInt((700 - 400) + 1) + 400;
creditReport.setCreditScore(new Integer(randomCreditScore));
return creditReport;
}
Step 5: Build the project
- Right click project -> Run as -> Maven Build
Goals: clean install
- Deploy the project to your JBoss EAP server
Test Web Service
To send a web request, point your browser at http://localhost:8080/simple-rest-service/rest/creditReport/get?fullname=YourName&ssn=666121923
You should get a response as a JSON dictionary
Great! Let's review what we just did.
Recap
Create Data Model
- We created a Credit Report model to send data from our web service
Create a REST Web Service
1: We added a descriptor to tell JBoss that we have a REST service (web.xml)
2: We added RESTEasy dependencies
- REST - a set of rules for creating web services
- JAX-RS - a Java API specification for REST
- RESTEasy - a Java framework for building REST Web services. It is an implementation of JAX-RS
3: We added JAX-RS annotations to create a CreditReport web service
- @Path - the path to our web service (/creditReport/get)
- @GET - this method will be used to retrieve data
- @Produces - the format of the data sent back to the client
- JSON (JavaScript Object Notation) - a format for transmitting data
- @QueryParam - this method has two parameters
Test Web Service
To access our web service, we took the base URL,
http://localhost:8080/simple-web-service
the URL pattern (web.xml)
/rest
the path to our method (@Path)
/creditReport/get
and our parameters
?fullname=YourName&ssn=666121923
to get our final URL
http://localhost:8080/simple-rest-service/rest/creditReport/get?fullname=YourName&ssn=666121923
Note: Usually an application would call the getCreditReport method, but for this example, we are viewing it in the browser.
What's a best practice for Web Services?
It's a good practice to choose JSON over XML to send data over a web service.
Since JSON is a more compact data format, your data will bent sent and processed faster than if you used XML.
So...
- Web services give us access to extra functionality without writing more code
- We can use JAX-RS annotations to define a RESTful web service
- Use JSON over XML to send/receive data quickly
Source Code & Useful Links
That's all for today!
Happy Coding,
-T.O.