-
-
Save anonymous/389e1e8064174bdcd102 to your computer and use it in GitHub Desktop.
SO Jersey TestBed.
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
package com.example; | |
import org.glassfish.grizzly.http.server.HttpServer; | |
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import java.io.IOException; | |
import java.net.URI; | |
public class Main | |
{ | |
public static final String BASE_URI = "http://localhost:8080"; | |
public static HttpServer startServer() | |
{ | |
final ResourceConfig rc = new ResourceConfig(); | |
rc.register(new MyBinder<Integer>(new MyProvider<Integer>() | |
{ | |
@Override | |
public Integer provide() | |
{ | |
return 123; | |
} | |
})); | |
rc.register(new MyResource()); | |
return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc); | |
} | |
public static void main(String[] args) throws IOException | |
{ | |
final HttpServer server = startServer(); | |
System.out.println(String.format("Jersey app started with WADL available at " | |
+ "%sapplication.wadl\nHit enter to stop it...", BASE_URI)); | |
System.in.read(); | |
server.stop(); | |
} | |
} | |
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
package com.example; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Target; | |
@Target({ ElementType.PARAMETER }) | |
public @interface MyAnnotation | |
{ | |
String value() default ""; | |
} |
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
package com.example; | |
import org.glassfish.hk2.api.InjectionResolver; | |
import org.glassfish.hk2.api.TypeLiteral; | |
import org.glassfish.hk2.utilities.binding.AbstractBinder; | |
public class MyBinder<T> extends AbstractBinder | |
{ | |
public MyBinder(MyProvider<T> myProvider) | |
{ | |
myResolver_m = new MyResolver<T>(myProvider); | |
} | |
@Override | |
protected void configure() | |
{ | |
System.err.printf("MyBinder.configure\n"); | |
bind(myResolver_m).to(new TypeLiteral<InjectionResolver<MyAnnotation>>(){}); | |
} | |
// PRIVATE | |
private final MyResolver<T> myResolver_m; | |
} | |
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
package com.example; | |
public interface MyProvider<T> | |
{ | |
public T provide(); | |
} | |
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
package com.example; | |
import org.glassfish.hk2.api.Injectee; | |
import org.glassfish.hk2.api.InjectionResolver; | |
import org.glassfish.hk2.api.ServiceHandle; | |
import javax.inject.Singleton; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Method; | |
@Singleton | |
public class MyResolver<T> implements InjectionResolver<MyAnnotation> | |
{ | |
public MyResolver(MyProvider provider) | |
{ | |
provider_m = provider; | |
} | |
@Override | |
public Object resolve(Injectee injectee, ServiceHandle<?> serviceHandle) | |
{ | |
System.err.printf("MyResolver.resolve\n"); | |
final MyAnnotation annotation = getAnnotation(injectee); | |
System.err.printf(" annotation.value() = %s\n", annotation.value()); | |
final T value = provider_m.provide(); | |
System.err.printf(" provider.provide() = %s\n", value.toString()); | |
return value; | |
} | |
@Override | |
public boolean isMethodParameterIndicator() | |
{ | |
return true; | |
} | |
@Override | |
public boolean isConstructorParameterIndicator() | |
{ | |
return false; | |
} | |
// PRIVATE | |
private static MyAnnotation getAnnotation(Injectee injectee) | |
{ | |
// NOTE: MyAnnotation can only annotate method parameter. | |
final Annotation[] annotations = ((Method)injectee.getParent()) | |
.getParameterAnnotations()[injectee.getPosition()]; | |
MyAnnotation annotation = null; | |
for (final Annotation a : annotations) | |
{ | |
if (a instanceof MyAnnotation) | |
{ | |
annotation = (MyAnnotation)a; | |
break; | |
} | |
} | |
return annotation; | |
} | |
private final MyProvider<T> provider_m; | |
} |
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
package com.example; | |
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; | |
@Path("myresource") | |
@Produces(MediaType.TEXT_PLAIN) | |
public class MyResource | |
{ | |
@GET | |
@Path("01/{value}") | |
public String get01(@PathParam("value") String value) | |
{ | |
return String.format("01: value = %s;\n", value); | |
} | |
@GET | |
@Path("02/{value}") | |
public String get02(@MyAnnotation Integer providedValue, @PathParam("value") String value) | |
{ | |
return String.format("02: value = %s; providedValue = %d\n", value, providedValue); | |
} | |
} |
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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.example</groupId> | |
<artifactId>simple-service</artifactId> | |
<packaging>jar</packaging> | |
<version>1.0-SNAPSHOT</version> | |
<name>simple-service</name> | |
<dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.glassfish.jersey</groupId> | |
<artifactId>jersey-bom</artifactId> | |
<version>${jersey.version}</version> | |
<type>pom</type> | |
<scope>import</scope> | |
</dependency> | |
</dependencies> | |
</dependencyManagement> | |
<dependencies> | |
<dependency> | |
<groupId>org.glassfish.jersey.containers</groupId> | |
<artifactId>jersey-container-grizzly2-http</artifactId> | |
</dependency> | |
<!-- uncomment this to get JSON support: | |
<dependency> | |
<groupId>org.glassfish.jersey.media</groupId> | |
<artifactId>jersey-media-moxy</artifactId> | |
</dependency> | |
--> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.9</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.jersey.core</groupId> | |
<artifactId>jersey-client</artifactId> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>2.5.1</version> | |
<inherited>true</inherited> | |
<configuration> | |
<source>1.6</source> | |
<target>1.6</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.codehaus.mojo</groupId> | |
<artifactId>exec-maven-plugin</artifactId> | |
<version>1.2.1</version> | |
<executions> | |
<execution> | |
<goals> | |
<goal>java</goal> | |
</goals> | |
</execution> | |
</executions> | |
<configuration> | |
<mainClass>com.example.Main</mainClass> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
<properties> | |
<jersey.version>2.2</jersey.version> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've tried this code with both 2.2 and 2.3.1 and I'm getting an error from Jeryey on the get02 resource method.
WARNING: The following warnings have been detected: WARNING: A HTTP GET method, public java.lang.String com.example.MyResource.get02(java.lang.Integer,java.lang.String), should not consume any entity.