Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit 251fa90

Browse files
Michal GajdosMarek Potociar
authored andcommitted
Refactored default-method-java8-webapp into java8-webapp.
Change-Id: I238a3aebb91ae65f2445b875ddd61c18ae7786ec Signed-off-by: Marek Potociar <[email protected]>
1 parent 440fd46 commit 251fa90

File tree

9 files changed

+193
-56
lines changed

9 files changed

+193
-56
lines changed

examples/default-method-java8-webapp/README.html renamed to examples/java8-webapp/README.html

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
<meta charset="utf-8">
4747
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
4848

49-
<title>Java8 Default Method WebApp Example</title>
49+
<title>JAX-RS and Java 8 WebApp Example</title>
5050

5151
<!-- Bootstrap -->
5252
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
@@ -57,21 +57,20 @@
5757
<div class="row">
5858
<div class="col-lg-offset-1 col-lg-10">
5959
<header class="page-header">
60-
<h1>Jersey Java8 Default Method Example</h1>
60+
<h1>Jersey and Java 8 Examples <small>Features and Types</small></h1>
6161
</header>
6262

6363
<p>
64-
This example demonstrates the usage of Java8's new construct - the default methods in interfaces.
65-
Default methods can be used as common JAX-RS resource methods.
64+
This example demonstrates the usage of Java 8's new constructs and types.
6665
</p>
6766
<p>
68-
The application is common web application, only combines methods and interfaces with default
69-
methods to show to show that this use case is possible.
70-
67+
Currently demonstrated are:
7168
</p>
69+
<ul>
70+
<li>Default methods in interfaces - acting as JAX-RS Resource Methods</li>
71+
</ul>
7272

7373
<h2>Contents</h2>
74-
7574
<p>
7675
The mapping of the URI path space is presented in the following table:
7776
</p>
@@ -88,35 +87,35 @@ <h2>Contents</h2>
8887

8988
<tbody>
9089
<tr>
91-
<td><code>/default-method/application.wadl</code></td>
92-
<td>Generated WADL</td>
93-
<td>GET</td>
94-
<td>returns XML with the application api description</td>
95-
</tr>
96-
<tr>
97-
<td><code>/default-method/compoundResource</code></td>
98-
<td>InterfaceResource</td>
90+
<td><code>/j8/default-method</code></td>
91+
<td>DefaultMethodInterface</td>
9992
<td>GET</td>
10093
<td>returns <code>interface-root</code> in <code>text/plain</code></td>
10194
</tr>
10295
<tr>
103-
<td><code>/default-method/compoundResource/path</code></td>
104-
<td>InterfaceResource</td>
96+
<td><code>/j8/default-method/path</code></td>
97+
<td>DefaultMethodInterface</td>
10598
<td>GET</td>
10699
<td>returns <code>interface-path</code> in <code>text/plain</code></td>
107100
</tr>
108101
<tr>
109-
<td><code>/default-method/compoundResource/class</code></td>
110-
<td>ClassResource</td>
102+
<td><code>/j8/default-method/class</code></td>
103+
<td>DefaultMethodResource</td>
111104
<td>GET</td>
112105
<td>returns <code>class</code> in <code>text/plain</code></td>
113106
</tr>
107+
<tr>
108+
<td><code>/j8/lambdas/{p}</code></td>
109+
<td>LambdaResource</td>
110+
<td>GET</td>
111+
<td>returns <code>{p}-lambdaized</code> in <code>text/plain</code></td>
112+
</tr>
114113
</tbody>
115114
</table>
116115

117116
<p>
118117
Application is Servlet 3 based, web.xml-less. Everything needed (resources/providers) is
119-
registered in the <code>DefaultMethodApplication</code> class.
118+
registered in the <code>Java8Application</code> class.
120119
</p>
121120

122121
</p>
@@ -131,10 +130,10 @@ <h2>Running the Example</h2>
131130
<p>
132131
This deploys current example using Jetty. You can access the application at:
133132
<ul>
134-
<li><a href="http://localhost:8080/default-method/application.wadl">http://localhost:8080/default-method/application.wadl</a></li>
135-
<li><a href="http://localhost:8080/default-method/compoundResource">http://localhost:8080/default-method/compoundResource</a></li>
136-
<li><a href="http://localhost:8080/default-method/compoundResource/path">http://localhost:8080/default-method/compoundResource/path</a></li>
137-
<li><a href="http://localhost:8080/default-method/compoundResource/class">http://localhost:8080/default-method/compoundResource/class</a></li>
133+
<li><a href="http://localhost:8080/j8/default-method">http://localhost:8080/j8/default-method</a></li>
134+
<li><a href="http://localhost:8080/j8/default-method/path">http://localhost:8080/j8/default-method/path</a></li>
135+
<li><a href="http://localhost:8080/j8/default-method/class">http://localhost:8080/j8/default-method/class</a></li>
136+
<li><a href="http://localhost:8080/j8/lambdas/test">http://localhost:8080/j8/lambdas/test</a></li>
138137
</ul>
139138
</p>
140139
</div>

examples/default-method-java8-webapp/pom.xml renamed to examples/java8-webapp/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@
5050
<version>2.18-SNAPSHOT</version>
5151
</parent>
5252

53-
<artifactId>default-method-java8-webapp</artifactId>
53+
<artifactId>java8-webapp</artifactId>
5454
<packaging>war</packaging>
55-
<name>jersey-examples-default-method-java8-webapp</name>
55+
<name>jersey-examples-java8-webapp</name>
5656

57-
<description>Jersey App Using Java8 Default Methods Example.</description>
57+
<description>Java 8 Types WebApp Example.</description>
5858

5959
<dependencies>
6060
<dependency>
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,25 @@
3838
* holder.
3939
*/
4040

41-
package org.glassfish.jersey.examples.defaultMethod;
41+
package org.glassfish.jersey.examples.java8;
4242

4343
import javax.ws.rs.ApplicationPath;
4444

45-
import org.glassfish.jersey.examples.defaultMethod.resources.ClassResource;
45+
import org.glassfish.jersey.examples.java8.resources.DefaultMethodResource;
46+
import org.glassfish.jersey.examples.java8.resources.LambdaResource;
4647
import org.glassfish.jersey.server.ResourceConfig;
4748

48-
4949
/**
50-
* Resource definitions for Default Method example.
50+
* Application for illustrating some of the features of Java 8 in JAX-RS.
5151
*
52-
* @author Adam Lindenthal (adam.lindenthal at oracle.com)
52+
* @author Michal Gajdos (michal.gajdos at oracle.com)
5353
*/
54-
@ApplicationPath("default-method")
55-
public class DefaultMethodApplication extends ResourceConfig {
54+
@ApplicationPath("j8")
55+
public class Java8Application extends ResourceConfig {
5656

57-
public DefaultMethodApplication() {
58-
register(ClassResource.class);
57+
public Java8Application() {
58+
// Resources.
59+
register(DefaultMethodResource.class);
60+
register(LambdaResource.class);
5961
}
6062
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* holder.
3939
*/
4040

41-
package org.glassfish.jersey.examples.defaultMethod.resources;
41+
package org.glassfish.jersey.examples.java8.resources;
4242

4343
import javax.ws.rs.GET;
4444
import javax.ws.rs.Path;
@@ -48,7 +48,7 @@
4848
*
4949
* @author Adam Lindenthal (adam.lindenthal at oracle.com)
5050
*/
51-
public interface InterfaceResource {
51+
public interface DefaultMethodInterface {
5252

5353
@GET
5454
default String root() {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* holder.
3939
*/
4040

41-
package org.glassfish.jersey.examples.defaultMethod.resources;
41+
package org.glassfish.jersey.examples.java8.resources;
4242

4343
import javax.ws.rs.GET;
4444
import javax.ws.rs.Path;
@@ -49,9 +49,9 @@
4949
*
5050
* @author Adam Lindenthal (adam.lindenthal at oracle.com)
5151
*/
52-
@Path("compoundResource")
52+
@Path("default-method")
5353
@Produces("text/plain")
54-
public class ClassResource implements InterfaceResource {
54+
public class DefaultMethodResource implements DefaultMethodInterface {
5555

5656
@GET
5757
@Path("class")
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.examples.java8.resources;
41+
42+
import java.util.Collections;
43+
import java.util.stream.Collectors;
44+
45+
import javax.ws.rs.GET;
46+
import javax.ws.rs.Path;
47+
import javax.ws.rs.PathParam;
48+
49+
/**
50+
* JAX-RS resource using Java SE 8 lambdas.
51+
*
52+
* @author Marek Potociar (marek.potociar at oracle.com)
53+
*/
54+
@Path("lambdas/{p}")
55+
public class LambdaResource {
56+
@GET
57+
public String getLambdaResult(@PathParam("p") String p) {
58+
return Collections.singleton(p).stream().map(v -> v + "-lambdaized").collect(Collectors.joining());
59+
}
60+
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@
3838
* holder.
3939
*/
4040

41-
package org.glassfish.jersey.examples.defaultMethod;
41+
package org.glassfish.jersey.examples.java8;
42+
43+
import javax.ws.rs.client.WebTarget;
44+
import javax.ws.rs.core.Application;
4245

43-
import org.glassfish.jersey.test.DeploymentContext;
4446
import org.glassfish.jersey.test.JerseyTest;
45-
import org.glassfish.jersey.test.ServletDeploymentContext;
4647

4748
import org.junit.Test;
4849
import static org.junit.Assert.assertEquals;
@@ -52,36 +53,35 @@
5253
*
5354
* @author Adam Lindenthal (adam.lindenthal at oracle.com)
5455
*/
55-
public class DefaultMethodTest extends JerseyTest {
56+
public class DefaultMethodResourceTest extends JerseyTest {
5657

5758
@Override
58-
protected DeploymentContext configureDeployment() {
59-
return ServletDeploymentContext.builder(DefaultMethodApplication.class).contextPath("default-method").build();
59+
protected Application configure() {
60+
return new Java8Application();
6061
}
6162

6263
/**
63-
* Test that JDK8 default methods do work as common JAX-RS resource methods
64+
* Test that JDK8 default methods do work as common JAX-RS resource methods.
6465
*/
6566
@Test
6667
public void testDefaultMethods() {
68+
final WebTarget defaultMethodTarget = target("default-method");
6769

6870
// test default method with no @Path annotation
69-
System.out.println("URI: " + target().getUri());
70-
String response = target("compoundResource").request().get(String.class);
71+
String response = defaultMethodTarget.request().get(String.class);
7172
assertEquals("interface-root", response);
7273

7374
// test default method with with @Path annotation
74-
response = target("compoundResource").path("path").request().get(String.class);
75+
response = defaultMethodTarget.path("path").request().get(String.class);
7576
assertEquals("interface-path", response);
7677
}
7778

7879
/**
79-
* Test, that resource methods defined in the class implementing the interface with default method do work normally
80-
* @throws Exception
80+
* Test, that resource methods defined in the class implementing the interface with default method do work normally.
8181
*/
8282
@Test
8383
public void testImplementingClass() throws Exception {
84-
final String response = target("compoundResource").path("class").request().get(String.class);
84+
final String response = target("default-method").path("class").request().get(String.class);
8585
assertEquals("class", response);
8686
}
8787
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.jersey.examples.java8;
42+
43+
import javax.ws.rs.client.WebTarget;
44+
import javax.ws.rs.core.Application;
45+
46+
import org.glassfish.jersey.test.JerseyTest;
47+
48+
import org.junit.Test;
49+
import static org.hamcrest.CoreMatchers.equalTo;
50+
import static org.junit.Assert.assertThat;
51+
52+
/**
53+
* Test usage of Java SE 8 lambdas in JAX-RS resource methods.
54+
*
55+
* @author Marek Potociar (marek.potociar at oracle.com)
56+
*/
57+
public class LambdaResourceTest extends JerseyTest {
58+
59+
@Override
60+
protected Application configure() {
61+
return new Java8Application();
62+
}
63+
64+
/**
65+
* Test that JDK8 lambdas do work in common JAX-RS resource methods.
66+
*/
67+
@Test
68+
public void testLambdas() {
69+
final WebTarget target = target("lambdas/{p}");
70+
71+
// test default method with no @Path annotation
72+
String response = target.resolveTemplate("p", "test").request().get(String.class);
73+
assertThat(response, equalTo("test-lambdaized"));
74+
}
75+
76+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@
896896
<jdk>1.8</jdk>
897897
</activation>
898898
<modules>
899-
<module>examples/default-method-java8-webapp</module>
899+
<module>examples/java8-webapp</module>
900900
<module>examples/rx-client-java8-webapp</module>
901901
<module>ext/rx/rx-client-java8</module>
902902
</modules>

0 commit comments

Comments
 (0)