BAEL-1552 - Jersey MVC Support

This commit is contained in:
Jon Cook 2018-07-29 17:22:14 +02:00 committed by José Carlos Valero Sánchez
parent 671bf20fe8
commit 1f0aabed07
10 changed files with 236 additions and 0 deletions

View File

@ -29,6 +29,28 @@
<artifactId>jaxrs-ri</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc-freemarker</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -41,6 +63,13 @@
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.jersey.server.http.EmbeddedHttpServer</mainClass>
</configuration>
</plugin>
</plugins>
</build>

View File

@ -0,0 +1,14 @@
package com.baeldung.jersey.server.config;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.freemarker.FreemarkerMvcFeature;
public class ViewApplicationConfig extends ResourceConfig {
public ViewApplicationConfig() {
packages("com.baeldung.jersey.server");
property(FreemarkerMvcFeature.TEMPLATE_BASE_PATH, "templates/freemarker");
register(FreemarkerMvcFeature.class);;
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.jersey.server.http;
import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import com.baeldung.jersey.server.config.ViewApplicationConfig;
public class EmbeddedHttpServer {
private static final URI BASE_URI = URI.create("http://localhost:8082/");
public static void main(String[] args) {
try {
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, new ViewApplicationConfig(), false);
Runtime.getRuntime()
.addShutdownHook(new Thread(() -> {
server.shutdownNow();
}));
server.start();
System.out.println(String.format("Application started.\nTry out %s\nStop the application using CTRL+C", BASE_URI + "fruit"));
} catch (IOException ex) {
Logger.getLogger(EmbeddedHttpServer.class.getName())
.log(Level.SEVERE, null, ex);
}
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.jersey.server.model;
public class Fruit {
private final String name;
private final String colour;
public Fruit(String name, String colour) {
this.name = name;
this.colour = colour;
}
public String getName() {
return name;
}
public String getColour() {
return colour;
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.jersey.server.rest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.server.mvc.ErrorTemplate;
import org.glassfish.jersey.server.mvc.Template;
import org.glassfish.jersey.server.mvc.Viewable;
import com.baeldung.jersey.server.model.Fruit;
@Path("/fruit")
public class FruitResource {
@GET
public Viewable get() {
return new Viewable("/index.ftl", "Fruit Index Page");
}
@GET
@Template(name = "/all.ftl")
@Path("/all")
@Produces(MediaType.TEXT_HTML)
public Map<String, Object> getAllFruit() {
final List<Fruit> fruits = new ArrayList<>();
fruits.add(new Fruit("banana", "yellow"));
fruits.add(new Fruit("apple", "red"));
fruits.add(new Fruit("kiwi", "green"));
final Map<String, Object> model = new HashMap<String, Object>();
model.put("items", fruits);
return model;
}
@GET
@ErrorTemplate(name = "/error.ftl")
@Template(name = "/named.ftl")
@Path("{name}")
@Produces(MediaType.TEXT_HTML)
public String getFruitByName(@PathParam("name") String name) {
if (!"banana".equalsIgnoreCase(name)) {
throw new IllegalArgumentException("Fruit not found: " + name);
}
return name;
}
}

View File

@ -0,0 +1,14 @@
<html>
<head>
<title>All fruit!</title>
</head>
<body>
<h1>All fruit!</h1>
<p>Fruits:</p>
<ul>
<#list items as fruit>
<li>${fruit.name}</li>
</#list>
</ul>
</body>
</html>

View File

@ -0,0 +1,8 @@
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Error - ${model.message}!</h1>
</body>
</html>

View File

@ -0,0 +1,8 @@
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome ${model}!</h1>
</body>
</html>

View File

@ -0,0 +1,8 @@
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Found fruit - ${model}!</h1>
</body>
</html>

View File

@ -0,0 +1,42 @@
package com.baeldung.jersey.server.rest;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.containsString;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;
import com.baeldung.jersey.server.config.ViewApplicationConfig;
public class FruitResourceIntegrationTest extends JerseyTest {
@Override
protected Application configure() {
return new ViewApplicationConfig();
}
@Test
public void testAllFruit() {
final String response = target("/fruit/all").request()
.get(String.class);
Assert.assertThat(response, allOf(containsString("banana"), containsString("apple"), containsString("kiwi")));
}
@Test
public void testIndex() {
final String response = target("/fruit").request()
.get(String.class);
Assert.assertThat(response, containsString("Welcome Fruit Index Page!"));
}
@Test
public void testErrorTemplate() {
final String response = target("/fruit/orange").request()
.get(String.class);
Assert.assertThat(response, containsString("Error - Fruit not found: orange!"));
}
}