Skip to content

Commit 06ceb4d

Browse files
saptarshibasupivovarit
authored andcommitted
JAX-RS API using Jersey [BAEL-558] (eugenp#956)
* WatchService vs. Apache Commons IO Mnitoring * Indentation fixed * Indentation fixed * JAX-RS API using Jersey [BAEL-558] * JAX-RS API using Jersey [BAEL-558] * Modifications made to remove xml * applicationContext.xml removed * All try catch moved to ExceptionMapper * fixes * review comments incorporated * module renamed
1 parent a5978cf commit 06ceb4d

16 files changed

Lines changed: 589 additions & 0 deletions

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<module>spring-hibernate3</module>
118118
<module>spring-hibernate4</module>
119119
<module>spring-integration</module>
120+
<module>spring-jersey</module>
120121
<module>spring-jms</module>
121122
<module>spring-jooq</module>
122123
<module>spring-jpa</module>

‎spring-jersey/.gitignore‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

‎spring-jersey/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
=========
2+
3+
## REST API with Jersey & Spring Example Project

‎spring-jersey/pom.xml‎

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>jersey-api</artifactId>
8+
<version>0.1-SNAPSHOT</version>
9+
<packaging>war</packaging>
10+
11+
<properties>
12+
<jersey.version>2.25</jersey.version>
13+
<jcl.slf4j.version>1.7.22</jcl.slf4j.version>
14+
<logback.version>1.1.8</logback.version>
15+
<junit.version>4.12</junit.version>
16+
<maven-war-plugin.version>3.0.0</maven-war-plugin.version>
17+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
18+
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
19+
<junit.version>4.12</junit.version>
20+
<httpcore.version>4.4.5</httpcore.version>
21+
<httpclient.version>4.5.2</httpclient.version>
22+
<servlet-api-version>3.1.0</servlet-api-version>
23+
</properties>
24+
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<configuration>
31+
<source>1.8</source>
32+
<target>1.8</target>
33+
</configuration>
34+
</plugin>
35+
<plugin>
36+
<artifactId>maven-war-plugin</artifactId>
37+
<version>${maven-war-plugin.version}</version>
38+
<configuration>
39+
<failOnMissingWebXml>false</failOnMissingWebXml>
40+
</configuration>
41+
</plugin>
42+
<plugin>
43+
<groupId>org.apache.maven.plugins</groupId>
44+
<artifactId>maven-surefire-plugin</artifactId>
45+
<version>${maven-surefire-plugin.version}</version>
46+
<configuration>
47+
<excludes>
48+
<exclude>**/*IntegrationTest.java</exclude>
49+
<exclude>**/*LiveTest.java</exclude>
50+
</excludes>
51+
</configuration>
52+
</plugin>
53+
54+
<plugin>
55+
<groupId>org.codehaus.cargo</groupId>
56+
<artifactId>cargo-maven2-plugin</artifactId>
57+
<version>${cargo-maven2-plugin.version}</version>
58+
<configuration>
59+
<wait>true</wait>
60+
<container>
61+
<containerId>jetty8x</containerId>
62+
<type>embedded</type>
63+
</container>
64+
<configuration>
65+
<properties>
66+
<cargo.servlet.port>8082</cargo.servlet.port>
67+
</properties>
68+
</configuration>
69+
</configuration>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
74+
<dependencies>
75+
76+
<!-- core library -->
77+
78+
<dependency>
79+
<groupId>org.glassfish.jersey.core</groupId>
80+
<artifactId>jersey-server</artifactId>
81+
<version>${jersey.version}</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.glassfish.jersey.containers</groupId>
85+
<artifactId>jersey-container-servlet</artifactId>
86+
<version>${jersey.version}</version>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.glassfish.jersey.media</groupId>
90+
<artifactId>jersey-media-json-jackson</artifactId>
91+
<version>${jersey.version}</version>
92+
</dependency>
93+
<!-- servlet api -->
94+
<dependency>
95+
<groupId>javax.servlet</groupId>
96+
<artifactId>javax.servlet-api</artifactId>
97+
<version>${servlet-api-version}</version>
98+
<scope>provided</scope>
99+
</dependency>
100+
<!-- optional library -->
101+
102+
<dependency>
103+
<groupId>org.glassfish.jersey.ext</groupId>
104+
<artifactId>jersey-spring3</artifactId>
105+
<version>${jersey.version}</version>
106+
<exclusions>
107+
<exclusion>
108+
<groupId>commons-logging</groupId>
109+
<artifactId>commons-logging</artifactId>
110+
</exclusion>
111+
</exclusions>
112+
</dependency>
113+
114+
<dependency>
115+
<groupId>org.slf4j</groupId>
116+
<artifactId>jcl-over-slf4j</artifactId>
117+
<version>${jcl.slf4j.version}</version>
118+
</dependency>
119+
120+
<dependency>
121+
<groupId>ch.qos.logback</groupId>
122+
<artifactId>logback-classic</artifactId>
123+
<version>${logback.version}</version>
124+
</dependency>
125+
126+
<!-- http client -->
127+
128+
<dependency>
129+
<groupId>org.apache.httpcomponents</groupId>
130+
<artifactId>httpclient</artifactId>
131+
<version>${httpclient.version}</version>
132+
<scope>test</scope>
133+
</dependency>
134+
135+
<dependency>
136+
<groupId>org.apache.httpcomponents</groupId>
137+
<artifactId>httpcore</artifactId>
138+
<version>${httpcore.version}</version>
139+
<scope>test</scope>
140+
</dependency>
141+
142+
<!-- junit -->
143+
144+
<dependency>
145+
<groupId>junit</groupId>
146+
<artifactId>junit</artifactId>
147+
<version>${junit.version}</version>
148+
<scope>test</scope>
149+
</dependency>
150+
</dependencies>
151+
152+
<profiles>
153+
<profile>
154+
<id>live</id>
155+
<build>
156+
<plugins>
157+
<plugin>
158+
<groupId>org.apache.maven.plugins</groupId>
159+
<artifactId>maven-surefire-plugin</artifactId>
160+
<executions>
161+
<execution>
162+
<phase>integration-test</phase>
163+
<goals>
164+
<goal>test</goal>
165+
</goals>
166+
<configuration>
167+
<excludes>
168+
<exclude>**/*IntegrationTest.java</exclude>
169+
</excludes>
170+
<includes>
171+
<include>**/*LiveTest.java</include>
172+
</includes>
173+
</configuration>
174+
</execution>
175+
</executions>
176+
<configuration>
177+
<systemPropertyVariables>
178+
<test.mime>json</test.mime>
179+
</systemPropertyVariables>
180+
</configuration>
181+
</plugin>
182+
<plugin>
183+
<groupId>org.codehaus.cargo</groupId>
184+
<artifactId>cargo-maven2-plugin</artifactId>
185+
<version>${cargo-maven2-plugin.version}</version>
186+
<configuration>
187+
<wait>false</wait>
188+
</configuration>
189+
<executions>
190+
<execution>
191+
<id>start-server</id>
192+
<phase>pre-integration-test</phase>
193+
<goals>
194+
<goal>start</goal>
195+
</goals>
196+
</execution>
197+
<execution>
198+
<id>stop-server</id>
199+
<phase>post-integration-test</phase>
200+
<goals>
201+
<goal>stop</goal>
202+
</goals>
203+
</execution>
204+
</executions>
205+
</plugin>
206+
</plugins>
207+
</build>
208+
</profile>
209+
</profiles>
210+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.server.config;
2+
3+
import javax.servlet.ServletContext;
4+
import javax.servlet.ServletException;
5+
6+
import org.springframework.core.Ordered;
7+
import org.springframework.core.annotation.Order;
8+
import org.springframework.web.WebApplicationInitializer;
9+
import org.springframework.web.context.ContextLoaderListener;
10+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
11+
12+
@Order(Ordered.HIGHEST_PRECEDENCE)
13+
public class ApplicationInitializer implements WebApplicationInitializer {
14+
15+
@Override
16+
public void onStartup(ServletContext servletContext) throws ServletException {
17+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
18+
servletContext.addListener(new ContextLoaderListener(context));
19+
servletContext.setInitParameter("contextConfigLocation", "com.baeldung.server");
20+
}
21+
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.server.config;
2+
3+
import javax.ws.rs.ApplicationPath;
4+
import javax.ws.rs.core.Application;
5+
6+
import com.baeldung.server.exception.AlreadyExistsExceptionHandler;
7+
import com.baeldung.server.exception.NotFoundExceptionHandler;
8+
import com.baeldung.server.rest.EmployeeResource;
9+
10+
import java.util.Arrays;
11+
import java.util.HashSet;
12+
import java.util.Set;
13+
14+
@ApplicationPath("/resources")
15+
public class RestConfig extends Application {
16+
public Set<Class<?>> getClasses() {
17+
return new HashSet<Class<?>>(Arrays.asList(EmployeeResource.class, NotFoundExceptionHandler.class, AlreadyExistsExceptionHandler.class));
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.server.exception;
2+
3+
import javax.ws.rs.core.Response;
4+
import javax.ws.rs.ext.ExceptionMapper;
5+
import javax.ws.rs.ext.Provider;
6+
7+
@Provider
8+
public class AlreadyExistsExceptionHandler implements ExceptionMapper<EmployeeAlreadyExists> {
9+
public Response toResponse(EmployeeAlreadyExists ex) {
10+
return Response.status(Response.Status.CONFLICT.getStatusCode()).build();
11+
}
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.server.exception;
2+
3+
public class EmployeeAlreadyExists extends RuntimeException {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.server.exception;
2+
3+
public class EmployeeNotFound extends RuntimeException {
4+
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.server.exception;
2+
3+
import javax.ws.rs.core.Response;
4+
import javax.ws.rs.ext.ExceptionMapper;
5+
import javax.ws.rs.ext.Provider;
6+
7+
@Provider
8+
public class NotFoundExceptionHandler implements ExceptionMapper<EmployeeNotFound> {
9+
public Response toResponse(EmployeeNotFound ex) {
10+
return Response.status(Response.Status.NOT_FOUND).build();
11+
}
12+
}

0 commit comments

Comments
 (0)