Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save natros/68ce47afe8a62ca93fd7 to your computer and use it in GitHub Desktop.

Select an option

Save natros/68ce47afe8a62ca93fd7 to your computer and use it in GitHub Desktop.

Revisions

  1. @zach-m zach-m revised this gist Apr 15, 2015. 5 changed files with 53 additions and 19 deletions.
    16 changes: 11 additions & 5 deletions Jetty 9 + Weld 2 + Jersey 2 + Jackson 2 + Maven
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,13 @@
    Below is a configuration for running Jetty 9 using its maven plugin.
    The web application itself relies on Jersey + Jackson for REST, and on Weld for CDI.
    As Jetty is a servlet-3.0 compatible container, no configuration is needed in web.xml.
    This is a template for creating and running a Jetty web application, using Jersey + Jackson for REST, and Weld for CDI.
    It is organized as a maven project, which builds a WAR file deployable to a standalone Jetty server.
    The Jetty maven plugin - which is more suitable for development time - is also configured in the pom.xml.

    IMPORTANT: Due to a bug in maven, it's required to use version 3.2.2 or above
    Comments:
    * As Jetty is a servlet-3.0 compatible container, no configuration is needed in web.xml
    * Due to a bug in maven, it's required to use version 3.2.2 or above
    * The JaxRs API classes are to be placed at the package - or below - the one where 'RestConfig.java' is
    * When using in standalone Jetty installation, enable the 'cdi' module before deploying
    >> java -jar start.jar --add-to-startd=cdi

    The tree structure is:

    @@ -20,4 +25,5 @@ The tree structure is:
    `-- WEB-INF
    |-- web.xml
    |-- jetty-env.xml
    `-- jetty-context.xml
    |-- jetty-context.xml
    `-- jetty-web.xml
    9 changes: 9 additions & 0 deletions jetty-context.xml
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,18 @@
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">

    <!-- configuration for WELD, as explained here: https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#_jetty -->
    <Set name="serverClasses">
    <Array type="java.lang.String">
    <Item>-org.eclipse.jetty.servlet.ServletContextHandler.Decorator</Item>
    </Array>
    </Set>

    <Get class="java.lang.System" name="out">
    <Call name="println">
    <Arg>*** JETTY-CONTEXT loaded</Arg>
    </Call>
    </Get>

    </Configure>
    9 changes: 9 additions & 0 deletions jetty-env.xml
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!-- <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> -->
    <Configure id="webAppCtx" class="org.eclipse.jetty.webapp.WebAppContext">

    <!-- configuration for WELD, as explained here: https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#_jetty -->
    <New id="BeanManager" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>
    <Ref id="webAppCtx" />
    @@ -14,4 +16,11 @@
    </New>
    </Arg>
    </New>

    <Get class="java.lang.System" name="out">
    <Call name="println">
    <Arg>*** JETTY-ENV loaded</Arg>
    </Call>
    </Get>

    </Configure>
    14 changes: 14 additions & 0 deletions jetty-web.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">

    <!-- configuration for deployment in standalone jetty (ignored by jetty-maven-plugin) -->
    <Set name="contextPath">/</Set>

    <Get class="java.lang.System" name="out">
    <Call name="println">
    <Arg>*** JETTY-WEB loaded</Arg>
    </Call>
    </Get>

    </Configure>
    24 changes: 10 additions & 14 deletions pom.xml
    Original file line number Diff line number Diff line change
    @@ -3,9 +3,9 @@
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    <groupId>com.tectonica</groupId>
    <artifactId>jetty9-weld2-jersey2-jackson2-maven</artifactId>
    <version>1</version>
    <packaging>war</packaging>

    <prerequisites>
    @@ -14,21 +14,17 @@

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <version.jetty>9.2.10.v20150310</version.jetty> <!-- latest in 9.2.x -->
    <version.jersey>2.14</version.jersey> <!-- latest for which 'jersey-gf-cdi-ban-custom-hk2-binding' exists -->
    <version.weld>2.2.5.Final</version.weld> <!-- same version used in stand-alone Jetty as the 'cdi' module -->

    <version.jetty>9.2.6.v20141205</version.jetty>
    <version.jersey>2.14</version.jersey>
    <version.weld>2.2.8.Final</version.weld>
    <!-- where needed, use the following: -->
    <version.servlet-api>3.0.1</version.servlet-api> <!-- based on what 'jersey-container-servlet' uses -->
    <version.jackson>2.3.2</version.jackson> <!-- based on what 'jersey-media-json-jackson' uses -->
    </properties>

    <dependencies>

    <!-- JavaEE -->
    <dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    </dependency>

    <!-- Jersey with Jackson -->
    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    @@ -57,7 +53,7 @@
    <artifactId>weld-servlet-core</artifactId>
    <version>${version.weld}</version>
    </dependency>

    </dependencies>

    <build>
  2. @zach-m zach-m revised this gist Jan 21, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Jetty 9 + Weld 2 + Jersey 2 + Jackson 2 + Maven
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,8 @@ Below is a configuration for running Jetty 9 using its maven plugin.
    The web application itself relies on Jersey + Jackson for REST, and on Weld for CDI.
    As Jetty is a servlet-3.0 compatible container, no configuration is needed in web.xml.

    IMPORTANT: Due to a bug in maven, it's required to use version 3.2.2 or above

    The tree structure is:

    |-- pom.xml
  3. @zach-m zach-m revised this gist Jan 21, 2015. 2 changed files with 21 additions and 1 deletion.
    1 change: 0 additions & 1 deletion Jetty 9 + Weld 2 + Jersey 2 + Jackson 2 + Maven
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    Below is
    21 changes: 21 additions & 0 deletions Jetty 9 + Weld 2 + Jersey 2 + Jackson 2 + Maven
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    Below is a configuration for running Jetty 9 using its maven plugin.
    The web application itself relies on Jersey + Jackson for REST, and on Weld for CDI.
    As Jetty is a servlet-3.0 compatible container, no configuration is needed in web.xml.

    The tree structure is:

    |-- pom.xml
    `-- src
    `-- main
    |-- java
    | `-- api
    | |-- RestConfig.java
    | `-- JsonConfig.java
    |-- resources
    | `-- META-INF
    | `-- beans.xml
    `-- webapp
    `-- WEB-INF
    |-- web.xml
    |-- jetty-env.xml
    `-- jetty-context.xml
  4. @zach-m zach-m revised this gist Jan 21, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Jetty 9 + Weld 2 + Jersey 2 + Jackson 2 + Maven
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Below is
  5. @zach-m zach-m created this gist Jan 21, 2015.
    30 changes: 30 additions & 0 deletions JsonConfig.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    package api;

    import javax.ws.rs.ext.ContextResolver;
    import javax.ws.rs.ext.Provider;

    import com.fasterxml.jackson.annotation.JsonInclude.Include;
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.ObjectMapper;

    @Provider
    public class JsonConfig implements ContextResolver<ObjectMapper>
    {
    final ObjectMapper mapper;

    public JsonConfig()
    {
    mapper = new ObjectMapper();
    // mapper.registerModule(new JaxbAnnotationModule());
    // mapper.registerModule(new JodaModule());
    mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    mapper.setSerializationInclusion(Include.NON_NULL);
    // mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }

    @Override
    public ObjectMapper getContext(Class<?> type)
    {
    return mapper;
    }
    }
    19 changes: 19 additions & 0 deletions RestConfig.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    package api;

    import javax.ws.rs.ApplicationPath;

    import org.glassfish.jersey.jackson.JacksonFeature;
    import org.glassfish.jersey.server.ResourceConfig;

    @ApplicationPath("v1")
    public class RestConfig extends ResourceConfig
    {
    public RestConfig()
    {
    // register with Jersey (assumes all Jersey resource classes are in this package or below)
    packages(this.getClass().getPackage().getName());

    // activate Jackson-based JSON support
    register(JacksonFeature.class);
    }
    }
    9 changes: 9 additions & 0 deletions beans.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <?xml version="1.0" encoding="UTF-8"?>

    <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_1_1.xsd"
    bean-discovery-mode="all">

    </beans>
    9 changes: 9 additions & 0 deletions jetty-context.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
    <Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="serverClasses">
    <Array type="java.lang.String">
    <Item>-org.eclipse.jetty.servlet.ServletContextHandler.Decorator</Item>
    </Array>
    </Set>
    </Configure>
    17 changes: 17 additions & 0 deletions jetty-env.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!-- <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd"> -->
    <Configure id="webAppCtx" class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="BeanManager" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>
    <Ref id="webAppCtx" />
    </Arg>
    <Arg>BeanManager</Arg>
    <Arg>
    <New class="javax.naming.Reference">
    <Arg>javax.enterprise.inject.spi.BeanManager</Arg>
    <Arg>org.jboss.weld.resources.ManagerObjectFactory</Arg>
    <Arg />
    </New>
    </Arg>
    </New>
    </Configure>
    97 changes: 97 additions & 0 deletions pom.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    <packaging>war</packaging>

    <prerequisites>
    <maven>3.2.2</maven>
    </prerequisites>

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <version.jetty>9.2.6.v20141205</version.jetty>
    <version.jersey>2.14</version.jersey>
    <version.weld>2.2.8.Final</version.weld>
    </properties>

    <dependencies>

    <!-- JavaEE -->
    <dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    </dependency>

    <!-- Jersey with Jackson -->
    <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>${version.jersey}</version>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${version.jersey}</version>
    </dependency>

    <!-- Weld for Jersey -->
    <dependency>
    <groupId>org.glassfish.jersey.containers.glassfish</groupId>
    <artifactId>jersey-gf-cdi</artifactId>
    <version>${version.jersey}</version>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.containers.glassfish</groupId>
    <artifactId>jersey-gf-cdi-ban-custom-hk2-binding</artifactId>
    <version>${version.jersey}</version>
    </dependency>
    <dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet-core</artifactId>
    <version>${version.weld}</version>
    </dependency>

    </dependencies>

    <build>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.2</version>
    <configuration>
    <source>1.7</source>
    <target>1.7</target>
    </configuration>
    </plugin>

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.4</version>
    </plugin>

    <!-- to execute: mvn clean package jetty:run -->
    <plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${version.jetty}</version>
    <configuration>
    <scanIntervalSeconds>2</scanIntervalSeconds>
    <contextXml>${project.basedir}/src/main/webapp/WEB-INF/jetty-context.xml</contextXml>
    <webApp>
    <jettyEnvXml>${project.basedir}/src/main/webapp/WEB-INF/jetty-env.xml</jettyEnvXml>
    </webApp>
    </configuration>
    </plugin>
    </plugins>
    </build>

    </project>
    6 changes: 6 additions & 0 deletions web.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <?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_3_0.xsd" id="WebApp_ID"
    version="3.0">

    </web-app>