Skip to content

Commit 1f0aabe

Browse files
JonCookCalamarBicefalo
authored andcommitted
BAEL-1552 - Jersey MVC Support
1 parent 671bf20 commit 1f0aabe

10 files changed

Lines changed: 236 additions & 0 deletions

File tree

‎jersey/pom.xml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,28 @@
2929
<artifactId>jaxrs-ri</artifactId>
3030
<version>${jersey.version}</version>
3131
</dependency>
32+
<dependency>
33+
<groupId>org.glassfish.jersey.containers</groupId>
34+
<artifactId>jersey-container-grizzly2-servlet</artifactId>
35+
<version>${jersey.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.glassfish.jersey.ext</groupId>
39+
<artifactId>jersey-mvc-freemarker</artifactId>
40+
<version>${jersey.version}</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.glassfish.jersey.test-framework</groupId>
44+
<artifactId>jersey-test-framework-core</artifactId>
45+
<version>${jersey.version}</version>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
50+
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
51+
<version>${jersey.version}</version>
52+
<scope>test</scope>
53+
</dependency>
3254
</dependencies>
3355

3456
<build>
@@ -41,6 +63,13 @@
4163
<failOnMissingWebXml>false</failOnMissingWebXml>
4264
</configuration>
4365
</plugin>
66+
<plugin>
67+
<groupId>org.codehaus.mojo</groupId>
68+
<artifactId>exec-maven-plugin</artifactId>
69+
<configuration>
70+
<mainClass>com.baeldung.jersey.server.http.EmbeddedHttpServer</mainClass>
71+
</configuration>
72+
</plugin>
4473
</plugins>
4574
</build>
4675

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.jersey.server.config;
2+
3+
import org.glassfish.jersey.server.ResourceConfig;
4+
import org.glassfish.jersey.server.mvc.freemarker.FreemarkerMvcFeature;
5+
6+
public class ViewApplicationConfig extends ResourceConfig {
7+
8+
public ViewApplicationConfig() {
9+
packages("com.baeldung.jersey.server");
10+
property(FreemarkerMvcFeature.TEMPLATE_BASE_PATH, "templates/freemarker");
11+
register(FreemarkerMvcFeature.class);;
12+
}
13+
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.jersey.server.http;
2+
3+
import java.io.IOException;
4+
import java.net.URI;
5+
import java.util.logging.Level;
6+
import java.util.logging.Logger;
7+
8+
import org.glassfish.grizzly.http.server.HttpServer;
9+
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
10+
11+
import com.baeldung.jersey.server.config.ViewApplicationConfig;
12+
13+
public class EmbeddedHttpServer {
14+
15+
private static final URI BASE_URI = URI.create("http://localhost:8082/");
16+
17+
public static void main(String[] args) {
18+
try {
19+
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, new ViewApplicationConfig(), false);
20+
21+
Runtime.getRuntime()
22+
.addShutdownHook(new Thread(() -> {
23+
server.shutdownNow();
24+
}));
25+
26+
server.start();
27+
28+
System.out.println(String.format("Application started.\nTry out %s\nStop the application using CTRL+C", BASE_URI + "fruit"));
29+
} catch (IOException ex) {
30+
Logger.getLogger(EmbeddedHttpServer.class.getName())
31+
.log(Level.SEVERE, null, ex);
32+
}
33+
34+
}
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.jersey.server.model;
2+
3+
public class Fruit {
4+
5+
private final String name;
6+
private final String colour;
7+
8+
public Fruit(String name, String colour) {
9+
this.name = name;
10+
this.colour = colour;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public String getColour() {
18+
return colour;
19+
}
20+
21+
22+
23+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.jersey.server.rest;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import javax.ws.rs.GET;
9+
import javax.ws.rs.Path;
10+
import javax.ws.rs.PathParam;
11+
import javax.ws.rs.Produces;
12+
import javax.ws.rs.core.MediaType;
13+
14+
import org.glassfish.jersey.server.mvc.ErrorTemplate;
15+
import org.glassfish.jersey.server.mvc.Template;
16+
import org.glassfish.jersey.server.mvc.Viewable;
17+
18+
import com.baeldung.jersey.server.model.Fruit;
19+
20+
@Path("/fruit")
21+
public class FruitResource {
22+
23+
@GET
24+
public Viewable get() {
25+
return new Viewable("/index.ftl", "Fruit Index Page");
26+
}
27+
28+
@GET
29+
@Template(name = "/all.ftl")
30+
@Path("/all")
31+
@Produces(MediaType.TEXT_HTML)
32+
public Map<String, Object> getAllFruit() {
33+
final List<Fruit> fruits = new ArrayList<>();
34+
fruits.add(new Fruit("banana", "yellow"));
35+
fruits.add(new Fruit("apple", "red"));
36+
fruits.add(new Fruit("kiwi", "green"));
37+
38+
final Map<String, Object> model = new HashMap<String, Object>();
39+
model.put("items", fruits);
40+
return model;
41+
}
42+
43+
@GET
44+
@ErrorTemplate(name = "/error.ftl")
45+
@Template(name = "/named.ftl")
46+
@Path("{name}")
47+
@Produces(MediaType.TEXT_HTML)
48+
public String getFruitByName(@PathParam("name") String name) {
49+
if (!"banana".equalsIgnoreCase(name)) {
50+
throw new IllegalArgumentException("Fruit not found: " + name);
51+
}
52+
return name;
53+
}
54+
55+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<title>All fruit!</title>
4+
</head>
5+
<body>
6+
<h1>All fruit!</h1>
7+
<p>Fruits:</p>
8+
<ul>
9+
<#list items as fruit>
10+
<li>${fruit.name}</li>
11+
</#list>
12+
</ul>
13+
</body>
14+
</html>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Welcome!</title>
4+
</head>
5+
<body>
6+
<h1>Error - ${model.message}!</h1>
7+
</body>
8+
</html>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Welcome!</title>
4+
</head>
5+
<body>
6+
<h1>Welcome ${model}!</h1>
7+
</body>
8+
</html>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Welcome!</title>
4+
</head>
5+
<body>
6+
<h1>Found fruit - ${model}!</h1>
7+
</body>
8+
</html>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.jersey.server.rest;
2+
3+
import static org.hamcrest.CoreMatchers.allOf;
4+
import static org.hamcrest.CoreMatchers.containsString;
5+
6+
import javax.ws.rs.core.Application;
7+
8+
import org.glassfish.jersey.test.JerseyTest;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
12+
import com.baeldung.jersey.server.config.ViewApplicationConfig;
13+
14+
public class FruitResourceIntegrationTest extends JerseyTest {
15+
16+
@Override
17+
protected Application configure() {
18+
return new ViewApplicationConfig();
19+
}
20+
21+
@Test
22+
public void testAllFruit() {
23+
final String response = target("/fruit/all").request()
24+
.get(String.class);
25+
Assert.assertThat(response, allOf(containsString("banana"), containsString("apple"), containsString("kiwi")));
26+
}
27+
28+
@Test
29+
public void testIndex() {
30+
final String response = target("/fruit").request()
31+
.get(String.class);
32+
Assert.assertThat(response, containsString("Welcome Fruit Index Page!"));
33+
}
34+
35+
@Test
36+
public void testErrorTemplate() {
37+
final String response = target("/fruit/orange").request()
38+
.get(String.class);
39+
Assert.assertThat(response, containsString("Error - Fruit not found: orange!"));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)