Forked from zach-m/Jetty 9 + Weld 2 + Jersey 2 + Jackson 2 + Maven
Last active
September 17, 2015 16:24
-
-
Save natros/68ce47afe8a62ca93fd7 to your computer and use it in GitHub Desktop.
Jetty 9 + Weld 2 + Jersey 2 + Jackson 2 + Maven
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
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: | |
|-- 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 | |
`-- jetty-web.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" /> | |
</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> | |
<Get class="java.lang.System" name="out"> | |
<Call name="println"> | |
<Arg>*** JETTY-ENV loaded</Arg> | |
</Call> | |
</Get> | |
</Configure> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>com.tectonica</groupId> | |
<artifactId>jetty9-weld2-jersey2-jackson2-maven</artifactId> | |
<version>1</version> | |
<packaging>war</packaging> | |
<prerequisites> | |
<maven>3.2.2</maven> | |
</prerequisites> | |
<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 --> | |
<!-- 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> | |
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment