Building RESTful WebService/Client using WildFly 8.0.0 Final, RESTEasy, and Maven Deployment

WildFly 8.0.0 Final was released recently. For a new project, I had to convert a legacy WebService into RESTful WebService using RESTEasy on JBoss Application Server. Not only converting RPC style JAX-WS WebService (Axis2-1.3), but I have to convert Ant base build/deployment process to Maven. My experience around RESTful WebService was around Jersey and Maven experience was limited before this project. As for my first step, I built a small prototype using WildFly + RESTEasy + Maven combination based on JEE7. There are several tutorials & resources that I could find in the internet, but it was difficult find a complete example. JBoss RESTEasy API and Java EE 7 tutorial have good reference and sample codes. However, I had difficult time finding the best practice based on my requirement. Most of the internet resources are either out of date or too simple for what I need to achieve. Also, most of examples are contained within the Eclipse environment, so considering deployment on a remote server was additional challenge . Eventually, I put things together and share my experience. 

Requirement

  • JDK 1.7 u51
  • Eclipse Keploer
  • WildFly 8.0.0 Final
  • Maven 3.1.1
  • RESTEasy 3.0.6 – comes with WildFly.

Objective

  1. Create a RESTful WebService using RESTEasy deploy to WildFly using Maven.
  2. Prototype should have a WebService and client.
  3. Build a web page that can be exposed by calling a WebService client.
  4. Retrieve a list of object by passing a list of String as a parameter.
  5. Application uses a single properties file fed from pom file based on environment profile. Assuming DEV/TEST/PROD environments are running on WildFly 8.0.0.
  6. Prototype will be deployed to WildFly standalone server. For this reason, portability wasn’t considered in this prototype.

pom.xml

  • WildFly plugin added
  • Two profiles are created ‘development’ and ‘prod’ to simulate different environment.
  • ${project.version} is a maven project property
  • ${maven.build.timestamp} is a special variable in maven
  • Most of JBoss specific jars are defined as ‘provided’ scope.

application.properties

environment=${env}
build_version=${build_version}
build_date=${timestamp}
build_number=${build_number}
student.restws.url=${student.restws.url}

After “mvn package -P development” command application.properties are populated as below.

environment=development
build_version=0.0.1-SNAPSHOT
build_date=2014-03-30 19:29:52
build_number=00001
student.restws.url=http://localhost:8080/RestWebServiceTest/rs/json

 web.xml

  • I wanted to prefix the RESTful WebService URL with ‘rs’, so I can differentiate normal Web Application from the WebService.

WebService Challenges

I need to retrieve a list of object based on user selected strings from list box. This list box has about 100 of items and there is a possibility that user can select “All” from the list box. There seem to be two ways of doing this : Convert string collection to JSON or comma delimited single string, use @PUT instead of @GET if string collection is too long. @PUT is for update, but I can safely pass the parameter in the body rather than URL as a @QueryParam. Also, I came across another way of doing this by passing long string parameter in the Header, but I couldn’t find an example code to follow. Especially, building WebService client passing long string to Header.

 

WebService Client

JAX-RS 2.0 defines client API, which makes it much easier to develop client side. I always felt developing RESTful WebService Client side is difficult because of different implementations use different classes/methods although underneath principle is the same.

Deployment

mvn clean compile package -P development wildfly:deploy-only

Source Code

https://github.com/mjtoolbox/RestWebServiceTest

Conclusion

I felt importance of RESTful WebService becomes bigger and bigger. It is due to modern Web Application development is moving away from server side generated front end pages. For this reason, I feel I need to improve hands on experience and understand the details. Some of learning points from this experience.

  • New JAX-RS 2.0 spec in JEE 7 made client side development easier. At least defines a standard way of implementation.
  • Working on the latest technology requires time and patient : much of the configuration resources are based on the old version.
  • Haven’t handled the security. Need to expand that later post.

 

 

 

 

 

Advertisements

10 thoughts on “Building RESTful WebService/Client using WildFly 8.0.0 Final, RESTEasy, and Maven Deployment

  1. Hi MJ, your work helped me a lot, but I wonder how to make it work in JBoss 7.1.1. I have a lot of trouble making REST to work on that platform… Any ideas? Thanks

    1. Nevermind, I found the way… Just change the WildFly Plugin by the JBoss Plugin:

      org.jboss.as.plugins
      jboss-as-maven-plugin
      7.7.Final

  2. Just getting started with your example.
    Question: is it still necessary to have RestEasy servlets in a web.xml with java 7?

    One note: I had to change the group id of the maven resource plugin from:
    org.apache.maven.plugin to org.apache.maven.plugins for it to be found in the maven repository.

  3. Great article bro you really made this implementation look very easy. Great job!
    Quick question, StringListConverter class which you are using in the service class, could you please tell us to which java utility package it belongs too? Is it part of RestEasy implementation? If you have customized it would you mind sharing the code please?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s