A bunch of Maven Archetypes for Spring based Projects

Maven is a good project management tool which greatly reduces the amount of time we spend on creating java projects with proper structure. With so many predefined maven archetypes it is even easier to create projects by simply selecting the archetype based on the technologies we need and type(jar/war/ear) of project we want to create.

However sometimes those predefined archetypes structure may not suite well for our needs or we may need some more additions to the pre-configured dependencies/frameworks etc.

Also in Eclipse the default java compiler level is 1.5 and configuring it to 1.6 every time is frustrating.
So I thought of creating some custom archetypes for the most common combination of technologies/frameworks that I frequently use.

Yes, I am aware of AppFuse which provides lot more maven archetypes. But the structure I follow is a bit different, so I thought of creating archetypes which suites my needs/style 🙂

Most of my projects are Spring based applications, so I have created the following templates so far:

1. Quickstart Java App : 
A Simple Java Application with JDK 1.6, SLF4J(Logback/Log4J) and failsafe plugin configuration.

2. Quickstart Web App:
A Simple Web Application with JDK 1.6, Servlet 2.5, SLF4J(Logback/Log4J) and failsafe plugin configuration.

3. Quickstart Spring App: 
A Java project with Spring 3.1.x, MySQL, JdbcTemplate and Log4j/Logback configuration.

4. Quickstart SpringMVC App: 
A Web project with SpringMVC 3.1.x,MySQL, JdbcTemplate, jQuery and Log4j/Logback configuration.

5. Quickstart SpringMVC-Tiles-SpringSecurity App: 
A Web project with SpringMVC 3.1.x, Apache Tiles, SpringSecurity 3.1.x, jQuery and Log4j/Logback configuration.

6. Quickstart SpringMVC-SiteMesh-SpringSecurity App: 
A Web project with SpringMVC 3.1.x, SiteMesh 2.x, SpringSecurity 3.1.x, jQuery and Log4j/Logback configuration.

7. Quickstart SpringMVC-JPA2(Hibernate) App:
A Web project with SpringMVC 3.1.x, JPA2(Hibernate4.x), SpringDataJPA, Apache Tiles, jQuery, SpringSecurity and Log4j/Logback configuration.

8. Quickstart SpringMVC-MyBatis App:
A Web project with SpringMVC 3.1.x, MyBatis, jQuery and Log4j/Logback configuration.

9. Quickstart Spring-JSF2(PrimeFaces)-JPA2(Hibernate) App:
A Web project with Spring 3.1.x, JSF2(PrimeFaces), JPA2(Hibernate),SpringDataJPA and Log4j/Logback configuration.

These are the archetypes so far I have completed and uploaded them onto my GitHub repository https://github.com/sivaprasadreddy/maven-archetype-templates

I have mentioned how to install them in your local repository in README file.

I am planning for writing some more template archetypes including SpringRESTFul Services, Spring-ApacheCXF App, Spring Integration, Spring-JavaEE6 etc.
Stay tuned 🙂

RESTEasy Tutorial Part 3 – Exception Handling


RESTEasy Tutorial Series

RESTEasy Tutorial Part-1: Basics

RESTEasy Tutorial Part-2: Spring Integration

RESTEasy Tutorial Part 3 – Exception Handling

Exception Handling is an obvious requirement while developing software application. If any error occured while processing user request we should show the user an error page with details like brief exception message, error code(optional), hints to correct the input and retry(optional) and actual root cause(optional). This is applicable to RESTful web services also.

But putting try-catch-finally blocks all around the code is not a good practice. We should design/code in such a way that if there is any unrecoverable error occured then the code should throw that exception and there should an exception handler to catch those exceptions and extract the error details and give a proper error response to the client with all the error details.

RESTEasy provides such ExceptionHandler mechanism which simplifies the ExceptionHandling process.

In this part I will show you how we can use RESTEasy’s ExceptionHandlers to handle Exceptions.

Step#1: Create Application Specific Exceptions.

Step#2: Create ExceptionHandlers by implementing ExceptionMapper interface.

Step#3: Update UserResource.getUserXMLById() method to validate user input and throw respective exceptions.

Step#4: Test the UserResource.getUserXMLById() service method by issueing following requests.

Important things to note:
As Spring is creating the necessary objects we should let Spring know about @Provider classes to get them registered with RESTEasy. We can do this in two ways.

a)Annotate Provider classes with @Component

b)Using component-scan’s include-filter.

<context:component-scan base-package=”com.sivalabs.springdemo”>
         <context:include-filter expression=”javax.ws.rs.ext.Provider” type=”annotation”/>

RESTEasy Tutorial Part-1: Basics


RESTEasy Tutorial Series

RESTEasy Tutorial Part-1: Basics

RESTEasy Tutorial Part-2: Spring Integration

RESTEasy Tutorial Part 3 – Exception Handling
RESTEasy is a JAX-RS implementation from JBoss/RedHat and is in-built in JBoss 6 onwards.
Here I am going to show you how to develop a Simple RESTful Web Services application using RESTEasy and JBossAS7.1.1.FINAL.

Step#1: Configure RESTEasy dependencies using Maven.

Step#2: Configure RESTEasy in web.xml

Step#3: Create User domain class, MockUserTable class to store User objects in-memory for testing purpose and UserResource class to expose CRUD operations on User as RESTful webservices.

Step#6: JUnit TestCase to test the REST Webservice.

Step#7: To test the REST service we can use the REST Client Tool. 
You can download REST Client Tool at http://code.google.com/a/eclipselabs.org/p/restclient-tool/

Important Things to Keep in mind:
1. org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap Listener should be registered before any other listener.

2. You should configure resteasy.servlet.mapping.prefix <context-param> if the HttpServletDispatcher servlet url-pattern is anything other than /*

3. Keep visiting my blog 🙂

Unable to create a maven project using appfuse archetypes?

I don’t know why sometimes when i tried to create a Maven project using one of appfuse archetypes, eclipse throws error saying unable to create maven project.

I resolved it by doing following:

Add Remote Catalog : http://repo1.maven.org/maven2/archetype-catalog.xml

Now I am able to create maven projects using Appfuse archetypes.

Solution for “Eclipse is running in a JRE, but a JDK is required” problem

When i installed Maven2 Plugin for eclipse i was getting the below error message when i startup my Eclipse IDE.
After that when I tried to perform Maven operations thrugh Eclipse I got some errors saying “…/tools.jar” is not there.

I did the below things to get rid of that problem.

open the eclipse.ini file and add the below argument.

Note: The VM configuration should be in two lines and in between –lancher and -vmargs.