JMiddleWay for Web Applications

DZone 's Guide to

JMiddleWay for Web Applications

Learn how to use this open-source Java framework to assist in the set up of the APIs you need to develop your next web application.

· Web Dev Zone ·
Free Resource

The Why

My intention is to provide a middleware framework which can be used to add REST APIs without worrying about involving several technologies and their various versions.  The user can define the architecture with JMiddleWay as the middleman between its UI and back-end services. You can choose the UI and back-end technologies of your choice and add the necessary code to JMiddleWay to integrate with your architecture.

The What

The best way to get to know the technology is to build a prototype with it. And that’s exactly what I did and what I will present in this post. JMiddleWay lets you create your own middleware quickly and add the required REST APIs easily. You can plug in your authentication module and configure how and where your middleware should log. Though fairly simple, the example highlights the most common annotations you’ll need to build your own REST API.

The main focus in the post will be on the Jersey JAX-RS implementation, all the other technologies are viewed as enablers.

Technologies Used

  • Spring 4.0.3
  • Jersey 2.8
  • Log4j 1.1.1
  • Maven 3.1
  • Tomcat 8

Follow Along

You can find all you'll need on GitHub.

Project Dependencies

Among other things, you need to have the Jersey Spring extension in your project’s classpath. You can easily add that with Maven by adding the following dependencies to the pom.xml file of the project:


Note: Jersey uses its own version of Spring libraries, so to use the ones you want, you need to exclude these libraries manually.

Code alert: If you want to see what other dependencies are used in the project you can download the complete pom.xml file from GitHub.

Web Application Deployment Descriptor – web.xml

<?xml version="1.0" encoding="UTF-8"?>
  xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <servlet-name>JMiddleWay - Middlwawre framework</servlet-name>
      <servlet-name>JMiddleWay - Middlwawre framework</servlet-name>


Notice the Jersey servlet configuration, the javax.ws.rs.application class, defines the components of the JAX-RS application.

Package com.jmiddleway.app:


 * Author  : Aravinda

 * Description :JAX RX Application class, Registers the components to be used by the JAX-RS application

 * Company :  

import java.util.logging.Logger;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.bridge.SLF4JBridgeHandler;
import com.jmiddleway.app.ExceptionMappers.JMiddleWayExceptionMapper;
import com.jmiddleway.app.ExceptionMappers.JMiddleWayGenericExceptionMapper;
import com.jmiddleway.app.filters.ApplicationContextProvider;
import com.jmiddleway.log.JMiddleWayLoggingFilter;
import com.jmiddleway.log.LoggerInjector;

public class JMiddleWayApp_JAXRS_app extends ResourceConfig {

   public JMiddleWayApp_JAXRS_app() {

  // Optionally remove existing handlers attached to j.u.l root logger
      SLF4JBridgeHandler.removeHandlersForRootLogger();  // (since SLF4J 1.6.5)

      // add SLF4JBridgeHandler to j.u.l's root logger, should be done once during
  // the initialization phase of your application

      registerInstances(new JMiddleWayLoggingFilter(Logger.getLogger(JMiddleWayApp_JAXRS_app.class.getName()), true));            

The class registers the following components:

  • JMiddleWayExceptionMapper implements the generic interface ExceptionMapper from the javax.ws.rs.ext package. This helps in generating uniform exception returning to the client if JMiddleWay or the back-end generates an exception defined by JMiddleWayException.
  • JMiddleWayGenericExceptionMapper implements the generic interface ExceptionMapper from the javax.ws.rs.ext package. This helps in generating uniform exception returning to the client if JMiddleWay or the back-end generates an exception other than JMiddleWayException.
  • ApplicationContextProvider - This class provides application-wide access to the Spring ApplicationContext! The ApplicationContext is injected in a static method of the class, "AppContext."

Spring Application Context Configuration

The Spring application context configuration is located in the applicationContext.xml classpath. 


                      http://www.springframework.org/schema/beans/spring-beans.xsd                          http://www.springframework.org/schema/tx
  <context:annotation-config />
  <task:annotation-driven />
  <context:component-scan base-package="com.jmiddleway.*" />
  <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj"

           proxy-target-class="false"  />
  <!--  JMiddleWayServletContext - provides a servelet context to help in autowire beans -->
  <bean id="jMiddleWayServletContext" class="com.jmiddleway.util.JMiddleWayServletContext"></bean>
  <!--  LoggerInjector - helps to inject Logger to any bean -->
  <bean class="com.jmiddleway.log.LoggerInjector" />
  <!--  Add your beans -->


Sample API:



public class YourResource {
   private YourResourceService yourResourceService;

   public Response GetYourResource(@Context HttpServletRequest request) throws JMiddleWayException {        
return yourResourceService.getYourResource(request);

   public Response CreateYourResource(@Context HttpServletRequest request) throws JMiddleWayException {
return yourResourceService.createYourResource(request);

   public Response UpdateYourResource(@Context HttpServletRequest request) throws JMiddleWayException {
return yourResourceService.updateYourResource(request);

   public Response DeleteYourResource(@Context HttpServletRequest request) throws JMiddleWayException {
return yourResourceService.deleteYourResource(request);

How to Use JMiddleWay

  • Create a new Dynamic web project using Eclipse.
    • Give your Project a name, such as JMiddleWay, and link to the location of the Git repository - - https://github.com/aravindatanneer/jmiddleway
    • Select the Target runtime for ex-Apache Tomcat 8.0.
    • Select Generate web.xml deployment descriptor.
    • Finish.
    • Right-click on the project and select configure to convert it to a Maven project – pom.xml is created.
    • Update the pom.xml and web.xml as mentioned above.
    • Add your rest APIs – copy the class, YourResource, and modify it according to your requirements.
    • Export the war file to the web app directory in Tomcat if your runtime is Tomcat.
    • See the logback.properties file in the classpath and change the location of the logfile. Currently, it is /tmp and the name of the file will be jmiddleway followed by the date. Also, check the logback.xml for the configuration of the logfile.
    • Test APIs using any REST client – I prefer the Chrome REST client.

If you have an authentication mechanism built-in to JMiddleWay, then write the test client by calling an API with the appropriate authentication mechanism.

java rest api, web dev, xml

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}