Install Guide for Servlet Containers

The simplest way to get started with MVC is to deploy your app to a JavaEE 8 application server. In this setup the application server will provider JAX-RS, CDI and BeaƄ Validation implemnetations for you. Please refer to Install Guide for Java EE for details about this setup.

However, there is a large number of users who prefer to run their applications on plain Servlet containers like Apache Tomcat and Jetty. In this setup you will have to provider JAX-RS, CDI and Bean Validations yourself.

In this guide we will show you how to run Ozark on Apache Tomcat using Weld, RESTEasy and Hibernate Validator.

The MVC specification and Ozark are under active development. Therefore, we recommend to use the latest snapshots for your experiments. This will ensure, that you are using the latest version of both the API and the implementation. Please refer to Using the latest snapshots to learn more.

Required dependencies

The following pom.xml example shows the dependency configuration for your application:

<!-- Get the API JARs for Java EE 8 -->
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-web-api</artifactId>
    <version>8.0</version>
</dependency>

<!-- Weld  -->
<dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet-core</artifactId>
    <version>3.0.5.Final</version>
</dependency>

<!-- RESTEasy -->
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-cdi</artifactId>
    <version>3.6.1.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-servlet-initializer</artifactId>
    <version>3.6.1.Final</version>
</dependency>

<!-- Bean Validation -->
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-validator-provider-11</artifactId>
    <version>3.6.1.Final</version>
</dependency>

<!-- MVC + Ozark for RESTEasy-->
<dependency>
    <groupId>javax.mvc</groupId>
    <artifactId>javax.mvc-api</artifactId>
    <version>1.0-pfd</version>
</dependency>
<dependency>
    <groupId>org.mvc-spec.ozark</groupId>
    <artifactId>ozark-resteasy</artifactId>
    <version>1.0.0-m04</version>
</dependency>

Configuration files

Make sure to add an empty beans.xml file in your /src/main/webapp/WEB-INF folder:

<?xml version="1.0"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
       version="2.0" bean-discovery-mode="all">

</beans>

Please also add a file called context.xml to src/main/resources/META-INF:

<?xml version="1.0" encoding="UTF-8"?>
<Context>

   <Resource name="BeanManager" auth="Container"
             type="javax.enterprise.inject.spi.BeanManager"
             factory="org.jboss.weld.resources.ManagerObjectFactory"/>

</Context>

This file context.xml is essential for the operation of the CDI in Tomcat as described in the Weld documentation.

The file to create is called web.xml and should be placed in the src/main/webapp/WEB-INF directory:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

  <listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
  </listener>

  <resource-env-ref>
    <resource-env-ref-name>BeanManager</resource-env-ref-name>
    <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
  </resource-env-ref>

  <!--http://docs.jboss.org/resteasy/docs/3.6.1.Final/userguide/html_single/index.html#d4e143 -->
  <context-param>
    <param-name>resteasy.injector.factory</param-name>
    <param-value>org.jboss.resteasy.cdi.CdiInjectorFactory</param-value>
  </context-param>

</web-app>

That’s all. Now you can start developing your first MVC 1.0 application.