March 27, 2016

How to run standalone Java EE MVC application

This project demonstrates how to create standalone Java EE MVC application.

MVC is newly developed Java EE JSR 371: Model-View-Controller (MVC 1.0) Specification.
The MVC API defines an action-oriented Web framework as an alternative to the component-oriented JSF. In an action-oriented framework, developers are responsible for all the controller logic and are given full control of the URI space for their application. 
The MVC API is layered on top of JAX-RS over Servlets and integrates with existing EE technologies like CDI and Bean Validation.
MVC spec is going to be part of next Java EE 8. MVC reference implementation named Ozark is hosted on MVC depends on Servlets, JAX-RS and CDI. I've chosen to use UndertowJersey and Weld. And DeltaSpike as CDI extension library. Finally, Gradle to build and run the project.

ext {
    undertowVersion = '1.3.18.Final'
    weldVersion = '2.3.3.Final'
    deltaspikeVersion = '1.5.4'
    jerseyVersion = '2.22.2'
    ozarkVersion = '1.0.0-m02'
task wrapper(type: Wrapper) {
    gradleVersion = '2.12'

Standalone CDI container

Weld contains module to support Java SE applications. Together with Apache DeltaSpike Java SE & Weld module:

dependencies {
    compile "$weldVersion"
    compile "org.apache.deltaspike.cdictrl:deltaspike-cdictrl-weld:$deltaspikeVersion"

it is easy to bootstrap and shutdown CDI container:

import javax.enterprise.context.ApplicationScoped;
import org.apache.deltaspike.cdise.api.CdiContainer;
import org.apache.deltaspike.cdise.api.CdiContainerLoader;
import org.jboss.weld.servlet.WeldInitialListener;
import org.jboss.weld.servlet.WeldTerminalListener;

public class Main {

    static CdiContainer init() throws ServletException, NoSuchMethodException {
        CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();



        return cdiContainer;

    static void destroy(CdiContainer cdiContainer) {


You can see we also start and stop CDI ApplicationScoped scope.

Initialize Servlet

Next we need to register Jersey ServletContainer as Undertow servlet:

dependencies {
    compile "io.undertow:undertow-core:$undertowVersion"
    compile "io.undertow:undertow-servlet:$undertowVersion"

    compile "org.glassfish.jersey.core:jersey-server:$jerseyVersion"
    compile "org.glassfish.jersey.containers:jersey-container-servlet-core:$jerseyVersion"
    compile "org.glassfish.jersey.ext.cdi:jersey-cdi1x-servlet:$jerseyVersion"

import javax.servlet.Servlet;
import javax.servlet.ServletException;

import io.undertow.Undertow;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;
import io.undertow.servlet.api.ServletContainer;
import io.undertow.servlet.api.ServletInfo;

import org.jboss.weld.servlet.WeldInitialListener;
import org.jboss.weld.servlet.WeldTerminalListener;

public class Main {


    private static void initServlet() throws ServletException, NoSuchMethodException {
        DeploymentInfo deploymentInfo = new DeploymentInfo()
                        createServletInfo("/resources/*", "JAX-RS Resources", org.glassfish.jersey.servlet.ServletContainer.class)

        ServletContainer servletContainer = Servlets.defaultContainer();
        DeploymentManager deploymentManager = servletContainer.addDeployment(deploymentInfo);

        Undertow server = Undertow.builder()
                .addHttpListener(8080, "")

    private static ServletInfo createServletInfo(String mapping, String servletName, Class<? extends Servlet> servlet)
            throws NoSuchMethodException {
        ServletInfo servletInfo = Servlets
                .servlet(servletName, servlet)

        return servletInfo;

Important part is registering of Weld Servlet listeners: WeldInitialListener and WeldTerminalListener.

The instance of Jersey ServletContainer is provided as ApplicationScoped bean by custom CdiProducer using CdiClassIntrospecter.

import javax.enterprise.inject.Produces;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;

public class CdiProducer {

    private long helloCounter;

    public ServletContainer createServletContainer() {
        final ResourceConfig resourceConfig = new ResourceConfig();
        resourceConfig.register(HelloController.class);"javax.mvc.engine.ViewEngine.viewFolder", "WEB-INF/views/");

        return new ServletContainer(resourceConfig);


You can see we need to customize "javax.mvc.engine.ViewEngine.viewFolder" property to support standalone application class-path behaviour.

CdiClassIntrospecter implements Undertow ClassIntrospecter interface and just delegates to CDI.


Now you can provide MVC @Controller methods:

dependencies {
    compile "org.glassfish.ozark:ozark:$ozarkVersion"
    compile "org.glassfish.ozark.ext:ozark-mustache:$ozarkVersion"

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.mvc.Models;
import javax.mvc.annotation.Controller;
import javax.mvc.annotation.View;

public class HelloController {

    private Models models;

    public void hello(@QueryParam("user") @DefaultValue("World") String user) {
        models.put("user", user);


You can see I've chosen to use Mustache template engine, see hello.mustache:

        <h1>Hello {{user}}!</h1>


Complete project sources are available on GitHub,

The easiest way how to run the project is:

gradlew run

The application is then available on: http://localhost:8080/resources/hello?user=Libor.


  1. This comment has been removed by the author.

  2. Thanks for sharing such a useful article JAVA coding is the best to develop applications


  3. Simply the manner in which I have anticipated. Your site truly is intriguing.
    training provider in malaysia

  4. Thank you for excellent article.You made an article that is interesting.
    data science course gurgaon

  5. nice blog!! i hope you will share a blog on Data Science.
    data analytics course in yelahanka

  6. great article!! sharing these type of articles is the nice one and i hope you will share an article on data science.By giving a institute like is one the best institute for doing certified courses
    data science course in aurangabad