Thomas Darimont
Erfahrenes Mitglied
Hallo,
hier mal ein Beispiel für ein einfaches JAX-RS Setup ohne web.xml mittels dynamischer Servlet Konfiguration (Servlet 3.0):
Unser WebBootstrap Interface:
Unser Boostraper -> Diese Klasse werden wir über den Java ServiceLoader Mechanismus laden lassen.
Dazu legen wir in dem Verzeichnis:
src/main/resources/META-INF/services die Datei "javax.servlet.ServletContainerInitializer" an.
Inhalt:
Hier unser RestBootstrap:
Hier noch eine Beispiel resource:
Mit dieser Konfiguration kann man dann die Beispiel Resource unter dieser URL (Web App Path anpassen!) aufrufen:
Unsere Maven pom.xml:
Gruß Tom
hier mal ein Beispiel für ein einfaches JAX-RS Setup ohne web.xml mittels dynamischer Servlet Konfiguration (Servlet 3.0):
Unser WebBootstrap Interface:
Java:
package de.tutorials.web.boot;
import javax.servlet.ServletContext;
public interface WebBoostrap {
void boot(ServletContext servletContext);
}
Unser Boostraper -> Diese Klasse werden wir über den Java ServiceLoader Mechanismus laden lassen.
Java:
package de.tutorials.web.boot;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import de.tutorials.web.boot.rest.RestBootstrap;
public class Bootstraper implements ServletContainerInitializer {
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
configureJaxRsProvider(servletContext);
}
private void configureJaxRsProvider(ServletContext sc) {
new RestBootstrap().boot(sc);
}
}
Dazu legen wir in dem Verzeichnis:
src/main/resources/META-INF/services die Datei "javax.servlet.ServletContainerInitializer" an.
Inhalt:
Code:
de.tutorials.web.boot.Bootstraper
Hier unser RestBootstrap:
Java:
package de.tutorials.web.boot.rest;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration.Dynamic;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.spi.container.ResourceFilters;
import de.tutorials.web.boot.WebBoostrap;
public class RestBootstrap implements WebBoostrap {
@Override
public void boot(ServletContext sc) {
configureJaxRsProvider(sc);
}
private void configureJaxRsProvider(ServletContext sc) {
Dynamic servletConfig = sc.addServlet("ServletAdaptor",com.sun.jersey.spi.container.servlet.ServletContainer.class);
servletConfig.setInitParameter(JSONConfiguration.FEATURE_POJO_MAPPING,"true");
//Register some Debugging / Tracing Filters
addResourceFilterTo(servletConfig,com.sun.jersey.api.container.filter.ResourceDebuggingFilterFactory.class.getName());
addRequestResponseFilterTo(servletConfig,com.sun.jersey.api.container.filter.LoggingFilter.class.getName(), true, true);
//Compression Filter to shrink requests / responses
addRequestResponseFilterTo(servletConfig,com.sun.jersey.api.container.filter.GZIPContentEncodingFilter.class.getName(), true, true);
servletConfig.setLoadOnStartup(1);
//All REST Resource Requests will be beneath /resources/...
servletConfig.addMapping("/resources/*");
}
private void addResourceFilterTo(Dynamic servletConfig,String resourceFilterClass) {
servletConfig.setInitParameter(ResourceFilters.class.getName(),resourceFilterClass);
}
private void addRequestResponseFilterTo(Dynamic servletConfig,String filterClassName, boolean filterRequest,boolean filterResponse) {
if (filterRequest) {
servletConfig.setInitParameter("com.sun.jersey.spi.container.ContainerRequestFilters",filterClassName);
}
if (filterResponse) {
servletConfig.setInitParameter("com.sun.jersey.spi.container.ContainerResponseFilters",filterClassName);
}
}
}
Hier noch eine Beispiel resource:
Java:
package de.tutorials.web.services;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
@Path("status")
public class StatusResource {
@GET
@Path("/")
public Response getStatus(){
return Response.ok("System: OK").build();
}
}
Mit dieser Konfiguration kann man dann die Beispiel Resource unter dieser URL (Web App Path anpassen!) aufrufen:
Code:
http://localhost:8080/de.tutorials.web/resources/status/
Unsere Maven pom.xml:
XML:
<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>de.tutorials</groupId>
<artifactId>de.tutorials.web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>de.tutorials.web</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jersey.version>1.12</jersey.version>
<servlet-api.version>3.0</servlet-api.version>
</properties>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-dependency-plugin
</artifactId>
<versionRange>
[2.1,)
</versionRange>
<goals>
<goal>copy</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Gruß Tom
Anhänge
Zuletzt bearbeitet von einem Moderator: