Last active
April 18, 2016 17:09
-
-
Save psamsotha/dfc9657290bb51b44ef640139974082b to your computer and use it in GitHub Desktop.
Inherit parameterized Path annnotation from interface. Stack Overflow http://stackoverflow.com/q/36682011/2587435
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.logging.Logger; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.core.Response; | |
import org.glassfish.jersey.filter.LoggingFilter; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.test.JerseyTest; | |
import org.junit.Test; | |
import static org.junit.Assert.assertEquals; | |
/** | |
* Stack Overflow question http://stackoverflow.com/q/36682011/2587435 | |
* | |
* Run this like any other JUnit test. Only one required test dependency | |
* | |
* <dependency> | |
* <groupId>org.glassfish.jersey.test-framework.providers</groupId> | |
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId> | |
* <version>2.22.2</version> | |
* <scope>test</scope> | |
* </dependency> | |
* | |
* @author Paul Samsotha | |
*/ | |
public class PathInheritTest extends JerseyTest { | |
public static interface IResource { | |
@GET | |
@Path("test/{id}") | |
public String get(@PathParam("id") String id); | |
} | |
@Path("concrete") | |
public static class ConcreteResource implements IResource { | |
@Override | |
public String get(String id) { | |
return id; | |
} | |
// The following fails as we are overriding the method parameter @PathParam annotation, | |
// which negates all interface annotations. | |
// | |
// @Override | |
// public String get(@PathParam("id") String id) { | |
// return id; | |
// } | |
} | |
@Override | |
public ResourceConfig configure() { | |
return new ResourceConfig(ConcreteResource.class) | |
.register(new LoggingFilter(Logger.getAnonymousLogger(), true)); | |
} | |
@Test | |
public void methodShouldGetAnnotation() { | |
final Response response = target("concrete/test/paul").request().get(); | |
assertEquals(200, response.getStatus()); | |
assertEquals("paul", response.readEntity(String.class)); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment