I'm beginning with jersey and trying to get freemarker working with it using TDD. I want to make a ViewProcessor for my templates, but fail to inject the servlet context in the class.
Here is the class code :
#Provider
public class myProcessor implements ViewProcessor<Template> {
[...]
#Context
public ServletContext myContext;
[...]
freemarkerConfiguration.setTemplateLoader(
new WebappTemplateLoader(myContext,
myContext.getInitParameter("freemarker.template.path")));
[...]
}
And here is the test code :
public class myProcessorTest extends JerseyTest {
public static myProcessor mp;
public myProcessorTest() throws Exception{
super(new WebAppDescriptor.Builder("com.domain").build());
}
#Test
public void firstTest(){
mp = new myProcessor();
String path = new String("test.ftl");
Template template = mp.resolve(path);
assertNotNull(template);
}
}
I use maven with dependencies as follow:
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-grizzly</artifactId>
<version>1.5-SNAPSHOT</version>
<scope>test</scope>
</dependency>
My code runs fine when I deploy to my local jetty server. But if I want to test the code in my IDE, it failed to inject the servlet context (#Context) : myContext is null when I run the test :/
I think I'm missing something, but I'm a complete beginner with servlet world.
Here's a technique for testing a specific resource class, using Jersey Test Framework, with servlet support. Also demonstrates how to customize the ServletContext.
import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.ServletDeploymentContext;
import org.glassfish.jersey.test.TestProperties;
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import static org.mockito.Mockito.mock;
/**
* A base class for testing web resources.
*/
public abstract class WebResourceTest extends JerseyTest {
/**
* Creates a JAX-RS resource configuration for test purposes.
*/
#Override
protected abstract ResourceConfig configure();
/**
* Creates a test container factory with servlet support.
*/
#Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new GrizzlyWebTestContainerFactory();
}
/**
* Configures a deployment context for JAX-RS.
*/
#Override
protected DeploymentContext configureDeployment() {
ResourceConfig app = configure();
app.register(new Feature() {
#Context
ServletContext servletContext;
#Override
public boolean configure(FeatureContext context) {
servletContext.setAttribute("example", new Object());
return true;
}
});
return ServletDeploymentContext.forServlet(new ServletContainer(app)).build();
}
}
A usage example:
import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.core.Context;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.core.Response;
import static org.mockito.Mockito.spy;
import static org.testng.Assert.assertEquals;
import static org.junit.Assert.*;
public class MyResourceTest extends WebResourceTest {
private MyResource resource;
#Override
protected ResourceConfig configure() {
resource = spy(new MyResource());
return new ResourceConfig().register(resource);
}
#Test
public void testSomething() {
Response r = target("/myresource").request().get();
assertEquals(200, r.getStatus());
assertEquals(1, resource.count);
}
}
#Path("/myresource")
public class MyResource {
int count = 0;
#Context
protected ServletContext servletContext;
#GET
public void get() {
Object attr = servletContext.getAttribute("example");
count++;
}
}
There's a couple of ways to do it. Remove the constructor and implement a configure() method like this:
public class myProcessorTest extends JerseyTest {
public static myProcessor mp;
#Override
protected AppDescriptor configure() {
return new WebAppDescriptor.Builder("com.domain")
.contextParam("contextConfigLocation", "classpath:/applicationContext.xml")
.contextPath("/").servletClass(SpringServlet.class)
.contextListenerClass(ContextLoaderListener.class)
.requestListenerClass(RequestContextListener.class)
.build();
}
or alternatively you can annotate your test with the spring context:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("classpath:applicationContext.xml")
public class MyProcessorTest extends JerseyTest {
public static myProcessor mp;
There is a solution to this problem that does not require spring, assuming you are using the default/standard Grizzy2 test framework provider. According to this answer the jersey-test-framework-provider-grizzly2 framework provider does not utilize a servlet environment in constructing the application context. Your symptoms result from there being no ServletContext instance to inject.
The workaround is to provide the test container for the unit tests yourself. First, modify your dependencies:
<!--<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.25</version>
<scope>test</scope>
</dependency>-->
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>2.25</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
<version>2.25</version>
</dependency>
Then, modify your test to provide a Grizzy servlet container:
#Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return (final URI baseUri, final DeploymentContext deploymentContext) ->
new TestContainer() {
private HttpServer server = null;
#Override
public ClientConfig getClientConfig() {
return null;
}
#Override
public URI getBaseUri() {
return baseUri;
}
#Override
public void start() {
try {
this.server = GrizzlyWebContainerFactory.create(baseUri, Collections
.singletonMap("jersey.config.server.provider.packages", "<your-package-name>"));
} catch (final ProcessingException | IOException cause) {
throw new TestContainerException(cause);
}
}
#Override
public void stop() {
this.server.shutdownNow();
}
};
}
I assume that you are going to use this in multiple unit tests, so it may be wise to extend JerseyTest so this common configuration may be performed automatically. Additionally, it may be worth reviewing org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory to see if there is any functionality provided by the test container that you wish to emulate/preserve. The example provided should be able to be dropped into your test to at least confirm this is a fix.
EDIT: In my own implementation, I required the ability to still supply a ResourceConfig when generating the server. I suspect that this is likely to be the common case for other Jersey Test Framework users. A working example of the proposed TestContainerFactory follows.
import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.spi.TestContainer;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.glassfish.jersey.test.spi.TestHelper;
public class RestTestContainerFactory implements TestContainerFactory {
public static class RestTestContainer implements TestContainer {
private static final Logger LOGGER = Logger.getLogger(RestTestContainer.class.getName());
private URI baseUri = null;
private final HttpServer server;
public RestTestContainer(final URI baseUri, final DeploymentContext context) {
this.baseUri = UriBuilder.fromUri(baseUri).path(context.getContextPath()).build();
if(LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Creating RestRestContainer configured at the base URI "+TestHelper.zeroPortToAvailablePort(baseUri));
}
try {
final WebappContext webContext = new WebappContext("TestContext", context.getContextPath());
context.getResourceConfig()
.register(new AbstractBinder() {
#Override
protected void configure() {
bind(webContext).to(ServletContext.class);
}
});
this.server = GrizzlyHttpServerFactory.createHttpServer(this.baseUri, context.getResourceConfig(), false);
webContext.deploy(this.server);
} catch (final ProcessingException cause) {
throw new TestContainerException(cause);
}
}
#Override
public ClientConfig getClientConfig() {
return null;
}
#Override
public URI getBaseUri() {
return baseUri;
}
#Override
public void start() {
if(server.isStarted()) {
LOGGER.warning("Ignoring start request - RestTestContainer is already started");
} else {
LOGGER.fine("Starting RestTestContainer...");
try {
server.start();
if(baseUri.getPort() == 0) {
baseUri = UriBuilder.fromUri(baseUri)
.port(server.getListener("grizzly").getPort())
.build();
LOGGER.info("Started GrizzlyTestContainer at the base URI "+baseUri);
}
}
catch(final ProcessingException | IOException cause) {
throw new TestContainerException(cause);
}
}
}
#Override
public void stop() {
if(server.isStarted()) {
LOGGER.fine("Stopping RestTestContainer...");
server.shutdownNow();
} else {
LOGGER.warning("Ignoring stop request - RestTestContainer is already stopped");
}
}
}
#Override
public TestContainer create(final URI baseUri, final DeploymentContext context) {
return new RestTestContainer(baseUri,context);
}
}
Frustratingly, grizzly's GrizzlyWebContainerFactory will provide a servlet context, but not configure with a resource config. Inversely, GrizzlyHttpServerFactory will configure an application with a ResourceConfig, but will not provide a web context.
We can work around this by creating the WebappContext (extends ServletContext) manually, configuring it, and then injecting it into the resource config by means of an AbstractBinder.
Related
I'm refactoring a legacy Java codebase to provide Guice-powered dependency injection to Jersey resource classes.
Here is a stripped down application that uses the legacy Jetty/Jersey setup (see Main & Application) along with my attempts to wire up Guice using their wiki article on servlets:
build.gradle
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.projectlombok:lombok:1.16.18'
compile 'com.google.inject:guice:4.1.0'
compile 'com.google.inject.extensions:guice-servlet:4.1.0'
compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.9.3'
compile 'org.eclipse.jetty:jetty-server:9.4.8.v20171121'
compile 'org.eclipse.jetty:jetty-servlet:9.4.8.v20171121'
compile 'org.glassfish.jersey.media:jersey-media-sse:2.26'
compile 'com.sun.jersey:jersey-servlet:1.19.4'
}
Main.java
package org.arabellan.sandbox;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.ServletModule;
import java.util.ArrayList;
import java.util.List;
public class Main {
static Injector injector;
public static void main(String[] args) throws Exception {
List<AbstractModule> modules = new ArrayList<>();
modules.add(new ExistingModule());
modules.add(new ServletModule());
injector = Guice.createInjector(modules);
injector.getInstance(Application.class).run();
}
}
Application.java
package org.arabellan.sandbox;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.google.inject.servlet.GuiceFilter;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.glassfish.jersey.message.DeflateEncoder;
import org.glassfish.jersey.message.GZipEncoder;
import org.glassfish.jersey.server.ResourceConfig;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.server.filter.EncodingFilter;
class Application {
void run() throws Exception {
Server jettyServer = new Server(8080);
ServletContextHandler httpContext = new ServletContextHandler(jettyServer, "/");
httpContext.addEventListener(new GuiceServletConfig());
httpContext.addFilter(GuiceFilter.class, "/*", null);
httpContext.addServlet(new ServletHolder(new ServletContainer(buildResourceConfig())), "/*");
jettyServer.setHandler(httpContext);
jettyServer.start();
}
private ResourceConfig buildResourceConfig() {
ResourceConfig config = new ResourceConfig();
config.register(JacksonJsonProvider.class);
config.registerClasses(EncodingFilter.class, GZipEncoder.class, DeflateEncoder.class);
config.packages("org.arabellan.sandbox");
return config;
}
}
ExistingModule.java
package org.arabellan.sandbox;
import com.google.inject.AbstractModule;
public class ExistingModule extends AbstractModule {
protected void configure() {
bind(FooDao.class).to(DynamoDBFooDao.class);
}
}
GuiceServletConfig.java
package org.arabellan.sandbox;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
public class GuiceServletConfig extends GuiceServletContextListener {
#Override
protected Injector getInjector() {
return Main.injector;
}
}
FooResource.java
package org.arabellan.sandbox;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/foo")
public class FooResource {
private final FooDao dao;
#Inject
public FooResource(FooDao dao) {
this.dao = dao;
}
#GET
#Path("/{id}")
public Response getById(#PathParam("id") String id) {
return Response.ok(dao.getById(id)).build();
}
}
DynamoDBFooDao.java
package org.arabellan.sandbox;
import javax.inject.Singleton;
#Singleton
public class DynamoDBFooDao implements FooDao {
public String getById(String id) {
return id;
}
}
FooDao.java
package org.arabellan.sandbox;
interface FooDao {
String getById(String id);
}
I'm failing to understand the various components and how they work together. As such I keep getting the following error:
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for constructor public org.arabellan.sandbox.FooResource(org.arabellan.sandbox.FooDao) at parameter index 0
If I access the Guice injector directly in FooResource's constructor then it works. This tells me the Jetty/Jersey stuff is setup properly to serve the resource and Guice is able to build it's dependency tree correctly. I believe this means the problem lies in getting Jersey to use Guice when constructing the resource.
As pointed out in the comments, I needed to settle on either version 1 or 2 of Jersey before trying to hook up Guice. I went with Jersey 2.
My original assumption however was correct, the linkage between Guice and Jersey (or rather HK2) needed to be setup. I facilitated this with the GuiceToHK2 class. I didn't want to define DI bindings in two places so this solution loops through all of the Guice bindings, filters them to a specific package (optional), and then binds them within HK2.
build.gradle
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.projectlombok:lombok:1.16.18'
compile 'com.google.inject:guice:4.1.0'
compile 'com.google.inject.extensions:guice-servlet:4.1.0'
compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.9.3'
compile 'org.eclipse.jetty:jetty-server:9.4.8.v20171121'
compile 'org.eclipse.jetty:jetty-servlet:9.4.8.v20171121'
compile 'org.glassfish.jersey.containers:jersey-container-jetty-servlet:2.26'
compile 'org.glassfish.jersey.media:jersey-media-sse:2.26'
compile 'org.glassfish.jersey.inject:jersey-hk2:2.26'
}
Application.java
package org.arabellan.sandbox;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.message.DeflateEncoder;
import org.glassfish.jersey.message.GZipEncoder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.filter.EncodingFilter;
import org.glassfish.jersey.servlet.ServletContainer;
class Application {
void run() throws Exception {
ServletContextHandler httpContext = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
ServletContainer container = new ServletContainer(buildResourceConfig());
ServletHolder holder = new ServletHolder(container);
httpContext.setContextPath("/");
httpContext.addServlet(holder, "/*");
Server jettyServer = new Server(8080);
jettyServer.setHandler(httpContext);
jettyServer.start();
}
private ResourceConfig buildResourceConfig() {
ResourceConfig config = new ResourceConfig();
config.register(new GuiceToHK2(Main.injector));
config.register(JacksonJsonProvider.class);
config.registerClasses(EncodingFilter.class, GZipEncoder.class, DeflateEncoder.class);
config.packages("org.arabellan.sandbox");
return config;
}
}
GuiceToHK2.java
package com.flightstats.hub.app;
import com.google.inject.Injector;
import com.google.inject.Key;
import lombok.extern.slf4j.Slf4j;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
#Slf4j
class GuiceToHK2 extends AbstractBinder {
private final Injector injector;
GuiceToHK2(Injector injector) {
this.injector = injector;
}
#Override
protected void configure() {
injector.getBindings().forEach((key, value) -> {
if (isNamedBinding(key)) {
bindNamedClass(key);
} else {
bindClass(key);
}
});
}
private boolean isNamedBinding(Key<?> key) {
return key.getAnnotationType() != null && key.getAnnotationType().getSimpleName().equals("Named");
}
private void bindClass(Key<?> key) {
try {
String typeName = key.getTypeLiteral().getType().getTypeName();
log.info("mapping guice to hk2: {}", typeName);
Class boundClass = Class.forName(typeName);
bindFactory(new ServiceFactory<>(boundClass)).to(boundClass);
} catch (ClassNotFoundException e) {
log.warn("unable to bind {}", key);
}
}
private void bindNamedClass(Key<?> key) {
try {
String typeName = key.getTypeLiteral().getType().getTypeName();
Method value = key.getAnnotationType().getDeclaredMethod("value");
String name = (String) value.invoke(key.getAnnotation());
log.info("mapping guice to hk2: {} (named: {})", typeName, name);
Class boundClass = Class.forName(typeName);
bindFactory(new ServiceFactory<>(boundClass)).to(boundClass).named(name);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
log.warn("unable to bind {}", key);
}
}
private class ServiceFactory<T> implements Factory<T> {
private final Class<T> serviceClass;
ServiceFactory(Class<T> serviceClass) {
this.serviceClass = serviceClass;
}
public T provide() {
return injector.getInstance(serviceClass);
}
public void dispose(T versionResource) {
// do nothing
}
}
}
It's not a bulletproof solution but it solved my issue. It assumes that everything that needs to be injected into my resources is in the org.arabellan.sandbox package and isn't #Named.
UPDATE: Made the solution more generic by removing assumptions.
hmmn for me it looks like you execute one of the following URLs:
http://localhost/foo
http://localhost/foo/
so that the string-parameter "id" of this function: "public Response getById(#PathParam("id") String id)" is null. which results in your error.
It's just an assumption. Could you check it if i'm right, please
I'm trying to intercept requests to my jaxrs apis basead on annotations, my filter is simple:
#Provider
public class Filter implements ContainerRequestFilter {
#Context
private ResourceInfo info;
#Override
public void filter(ContainerRequestContext crc) throws IOException {
// here I'm trying to get the annotate resource class or method.
info.getResourceClass().isAnnotationPresent(MyCustomAnnotation.class);
}
}
this works fine with a simple resource like this: (works both in class and method)
#Path("/")
public class SimpleResource {
#GET
#MyCustomAnnotation
public String test() {
return "test";
}
}
But in my real application, I have scenarios like this:
#Path("/")
public class RootResource {
#Inject
ChildResource childResource;
#Path("child")
public ChildResource child () {
return childResource;
}
}
So, I wanna put my custom annotation only on ResourceLocator and on the fly verify that the final resource contains the annotation.
#Path("/")
#CustomAnnotation
public class RootResource {
#Inject
ChildResource childResource;
#Path("child")
public ChildResource child () {
return childResource;
}
}
is it possible? or i can only get information about the matched resource?
"In jersey how would be this?"
With Jersey you have access to the resource model, and ways to traverse the model. You can see jersey server introspectionmodeller not public in v2.0? for some explanation and examples of how to traverse the model and Resource and ResourceMethod. Other than that, there is not much documentation these APIs.
Below is a complete example Using Jersey Test Framework. You can run the class like any other JUnit test. You just need this one dependency to run it
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.19</version>
<scope>test</scope>
</dependency>
And here's the test.
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;
import java.util.logging.Logger;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import static junit.framework.Assert.assertEquals;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ExtendedUriInfo;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.model.Resource;
import org.glassfish.jersey.server.model.ResourceMethod;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
public class ResourceModelTest extends JerseyTest {
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
public static #interface ResourceAnnotation {
String value();
}
#Path("root")
#ResourceAnnotation("SomeValue")
public static class ParentResource {
#Path("sub")
public ChildResource getChild() {
return new ChildResource();
}
#GET
public String get() {
return "ROOT";
}
}
public static class ChildResource {
#GET
public String get() {
return "CHILD";
}
}
#Provider
public static class ResourceFilter implements ContainerResponseFilter {
#Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
ExtendedUriInfo info = (ExtendedUriInfo) requestContext.getUriInfo();
List<ResourceMethod> resourceLocators = info.getMatchedResourceLocators();
if (!resourceLocators.isEmpty()) {
Resource parent = resourceLocators.get(0).getParent();
Class<?> parentClass = parent.getHandlerClasses().iterator().next();
ResourceAnnotation anno = parentClass.getAnnotation(ResourceAnnotation.class);
if (anno != null) {
responseContext.getHeaders().putSingle("X-SubResource-Header", anno.value());
}
}
}
}
#Override
public ResourceConfig configure() {
return new ResourceConfig(ParentResource.class)
.register(ResourceFilter.class);
}
#Override
public void configureClient(ClientConfig config) {
config.register(new LoggingFilter(Logger.getAnonymousLogger(), true));
}
#Test
public void get_child_resource() {
Response response = target("root/sub").request().get();
assertEquals(200, response.getStatus());
assertEquals("SomeValue", response.getHeaderString("X-SubResource-Header"));
}
}
Hello I am building an application using dropwizard, that is using jersey 2.16 internally as REST API framework.
For the whole application on all resource methods I need some information so to parse that information I defined a custom filter like below
#java.lang.annotation.Target(ElementType.PARAMETER)
#java.lang.annotation.Retention(RetentionPolicy.RUNTIME)
public #interface TenantParam {
}
The tenant factory is defined below
public class TenantFactory implements Factory<Tenant> {
private final HttpServletRequest request;
private final ApiConfiguration apiConfiguration;
#Inject
public TenantFactory(HttpServletRequest request, #Named(ApiConfiguration.NAMED_BINDING) ApiConfiguration apiConfiguration) {
this.request = request;
this.apiConfiguration = apiConfiguration;
}
#Override
public Tenant provide() {
return null;
}
#Override
public void dispose(Tenant tenant) {
}
}
I haven't actually implemented the method but structure is above. There is also a TenantparamResolver
public class TenantParamResolver implements InjectionResolver<TenantParam> {
#Inject
#Named(InjectionResolver.SYSTEM_RESOLVER_NAME)
private InjectionResolver<Inject> systemInjectionResolver;
#Override
public Object resolve(Injectee injectee, ServiceHandle<?> serviceHandle) {
if(Tenant.class == injectee.getRequiredType()) {
return systemInjectionResolver.resolve(injectee, serviceHandle);
}
return null;
}
#Override
public boolean isConstructorParameterIndicator() {
return false;
}
#Override
public boolean isMethodParameterIndicator() {
return true;
}
}
Now in my resource method I am doing like below
#POST
#Timed
public ApiResponse create(User user, #TenantParam Tenant tenant) {
System.out.println("resource method invoked. calling service method");
System.out.println("service class" + this.service.getClass().toString());
//DatabaseResult<User> result = this.service.insert(user, tenant);
//return ApiResponse.buildWithPayload(new Payload<User>().addObjects(result.getResults()));
return null;
}
Here is how I am configuring the application
#Override
public void run(Configuration configuration, Environment environment) throws Exception {
// bind auth and token param annotations
environment.jersey().register(new AbstractBinder() {
#Override
protected void configure() {
bindFactory(TenantFactory.class).to(Tenant.class);
bind(TenantParamResolver.class)
.to(new TypeLiteral<InjectionResolver<TenantParam>>() {})
.in(Singleton.class);
}
});
}
The problem is during application start I am getting below error
WARNING: No injection source found for a parameter of type public void com.proretention.commons.auth.resources.Users.create(com.proretention.commons.api.core.Tenant,com.proretention.commons.auth.model.User) at index 0.
and there is very long stack error stack and description
Below is the declaration signature of user pojo
public class User extends com.company.models.Model {
No annotations on User class. Model is a class that defines only single property id of type long and also no annotations on model class
When I remove the User parameter from above create resource method it works fine and when I removed TenantParam it also works fine. The problem only occurs when I use both User and TenantParam
What I am missing here ? how to resolve this error ?
EDITED
I just tried with two custom method param injection, that is also not working
#POST
#Path("/login")
#Timed
public void validateUser(#AuthParam AuthToken token, #TenantParam Tenant tenant) {
}
What I am missing here ? Is this a restriction in jersey ?
Method parameters are handled a little differently for injection. The component we need to implement for this, is the ValueFactoryProvider. Once you implement that, you also need to bind it in your AbstractBinder.
Jersey has a pattern that it follows for implementing the ValueFactoryProvider. This is the pattern used to handle parameters like #PathParam and #QueryParam. Jersey has a ValueFactoryProvider for each one of those, as well as others.
The pattern is as follows:
Instead of implementing the ValueFactoryProvider directly, we extend AbstractValueFactoryProvider
public static class TenantValueProvider extends AbstractValueFactoryProvider {
#Inject
public TenantValueProvider(MultivaluedParameterExtractorProvider mpep,
ServiceLocator locator) {
super(mpep, locator, Parameter.Source.UNKNOWN);
}
#Override
protected Factory<?> createValueFactory(Parameter parameter) {
if (!parameter.isAnnotationPresent(TenantParam.class)
|| !Tenant.class.equals(parameter.getRawType())) {
return null;
}
return new Factory<Tenant>() {
#Override
public Tenant provide() {
...
}
};
}
In this component, it has a method we need to implement that returns the Factory that provides the method parameter value.
The InjectionResolver is what is used to handle the custom annotation. With this pattern, instead of directly implementing it, as the OP has, we just extend ParamInjectionResolver passing in our AbstractValueFactoryProvider implementation class to super constructor
public static class TenantParamInjectionResolver
extends ParamInjectionResolver<TenantParam> {
public TenantParamInjectionResolver() {
super(TenantValueProvider.class);
}
}
And that's really it. Then just bind the two components
public static class Binder extends AbstractBinder {
#Override
public void configure() {
bind(TenantParamInjectionResolver.class)
.to(new TypeLiteral<InjectionResolver<TenantParam>>(){})
.in(Singleton.class);
bind(TenantValueProvider.class)
.to(ValueFactoryProvider.class)
.in(Singleton.class);
}
}
Below is a complete test using Jersey Test Framework. The required dependencies are listed in the javadoc comments. You can run the test like any other JUnit test
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.logging.Logger;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.api.InjectionResolver;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.TypeLiteral;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.internal.inject.AbstractContainerRequestValueFactory;
import org.glassfish.jersey.server.internal.inject.AbstractValueFactoryProvider;
import org.glassfish.jersey.server.internal.inject.MultivaluedParameterExtractorProvider;
import org.glassfish.jersey.server.internal.inject.ParamInjectionResolver;
import org.glassfish.jersey.server.model.Parameter;
import org.glassfish.jersey.server.spi.internal.ValueFactoryProvider;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Stack Overflow https://stackoverflow.com/q/29145807/2587435
*
* Run this like any other JUnit test. Dependencies required are as the following
*
* <dependency>
* <groupId>org.glassfish.jersey.test-framework.providers</groupId>
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
* <version>2.22</version>
* <scope>test</scope>
* </dependency>
* <dependency>
* <groupId>org.glassfish.jersey.media</groupId>
* <artifactId>jersey-media-json-jackson</artifactId>
* <version>2.22</version>
* <scope>test</scope>
* </dependency>
*
* #author Paul Samsotha
*/
public class TenantInjectTest extends JerseyTest {
#Target(ElementType.PARAMETER)
#Retention(RetentionPolicy.RUNTIME)
public static #interface TenantParam {
}
public static class User {
public String name;
}
public static class Tenant {
public String name;
public Tenant(String name) {
this.name = name;
}
}
public static class TenantValueProvider extends AbstractValueFactoryProvider {
#Inject
public TenantValueProvider(MultivaluedParameterExtractorProvider mpep,
ServiceLocator locator) {
super(mpep, locator, Parameter.Source.UNKNOWN);
}
#Override
protected Factory<?> createValueFactory(Parameter parameter) {
if (!parameter.isAnnotationPresent(TenantParam.class)
|| !Tenant.class.equals(parameter.getRawType())) {
return null;
}
return new AbstractContainerRequestValueFactory<Tenant>() {
// You can #Inject things here if needed. Jersey will inject it.
// for example #Context HttpServletRequest
#Override
public Tenant provide() {
final ContainerRequest request = getContainerRequest();
final String name
= request.getUriInfo().getQueryParameters().getFirst("tenent");
return new Tenant(name);
}
};
}
public static class TenantParamInjectionResolver
extends ParamInjectionResolver<TenantParam> {
public TenantParamInjectionResolver() {
super(TenantValueProvider.class);
}
}
public static class Binder extends AbstractBinder {
#Override
public void configure() {
bind(TenantParamInjectionResolver.class)
.to(new TypeLiteral<InjectionResolver<TenantParam>>(){})
.in(Singleton.class);
bind(TenantValueProvider.class)
.to(ValueFactoryProvider.class)
.in(Singleton.class);
}
}
}
#Path("test")
#Produces("text/plain")
#Consumes("application/json")
public static class TestResource {
#POST
public String post(User user, #TenantParam Tenant tenent) {
return user.name + ":" + tenent.name;
}
}
#Override
public ResourceConfig configure() {
return new ResourceConfig(TestResource.class)
.register(new TenantValueProvider.Binder())
.register(new LoggingFilter(Logger.getAnonymousLogger(), true));
}
#Test
public void shouldReturnTenantAndUserName() {
final User user = new User();
user.name = "peeskillet";
final Response response = target("test")
.queryParam("tenent", "testing")
.request()
.post(Entity.json(user));
assertEquals(200, response.getStatus());
assertEquals("peeskillet:testing", response.readEntity(String.class));
}
}
See Also:
Jersey 2.x Custom Injection Annotation With Attributes
My Comment in the Dropwizard issue: "No injection source found for a parameter"
I am trying to follow the example located here to create a factory in order to inject my HttpSession. Unfortunately no matter what I try it is not working. Not sure what could be the issue.
I have tried injecting just the HttpServletRequest and a provider. Here is my example using a provider. The error is a null pointer exception when trying to access the provider in the provide method. If I try to inject the HttpServletRequest I get no object available for injection. I am running this inside the GrizzlyTestContainer using JerseyTest. Is there something I need to add to my binder in order to bind the HttpServletRequest? I cannot seem to find an example.
public class HttpSessionFactory implements Factory<HttpSession> {
private final HttpServletRequest request;
#Inject
public HttpSessionFactory(Provider<HttpServletRequest> requestProvider) {
this.request = requestProvider.get();
}
#Override
public HttpSession provide() {
return request.getSession();
}
#Override
public void dispose(HttpSession t) {
}
}
You should #Override protected DeploymentContext configureDeployment() in the JerseyTest to return a ServletDeploymentContext. For example
import javax.inject.Inject;
import javax.inject.Provider;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.ServletDeploymentContext;
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.Test;
public class ServletTest extends JerseyTest {
#Path("/session")
public static class SessionResource {
#Inject
HttpSession session;
#GET
public Response getSessionId() {
return Response.ok(session.getId()).build();
}
}
public static class HttpSessionFactory implements Factory<HttpSession> {
private final HttpServletRequest request;
#Inject
public HttpSessionFactory(Provider<HttpServletRequest> requestProvider) {
this.request = requestProvider.get();
}
#Override
public HttpSession provide() {
return request.getSession();
}
#Override
public void dispose(HttpSession t) {
}
}
#Override
protected TestContainerFactory getTestContainerFactory() {
return new GrizzlyWebTestContainerFactory();
}
#Override
protected DeploymentContext configureDeployment() {
ResourceConfig config = new ResourceConfig(SessionResource.class);
config.register(new AbstractBinder() {
#Override
protected void configure() {
bindFactory(HttpSessionFactory.class).to(HttpSession.class);
}
});
return ServletDeploymentContext.forServlet(
new ServletContainer(config)).build();
}
#Test
public void test() {
System.out.println(target("session").request().get(String.class));
}
}
You can see more examples in the source code tests
I am using "Jersey Test Framework" for unit testing my webservice.
Here is my resource class :
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
// The Java class will be hosted at the URI path "/helloworld"
#Path("/helloworld")
public class class HelloWorldResource {
private SomeService service;
#GET
#Produces("text/plain")
public String getClichedMessage() {
// Return some cliched textual content
String responseFromSomeService = service.getSomething();
return responseFromSomeService;
}
}
How can I mock SomeService in unit tests ?
See Update below: You don't need a Factory
If you are using Jersey 2, one solution would be to use Custom Injection and Lifecycle Management feature (with HK2 - which comes with the Jersey dist). Also required would be a Mocking framework of course. I'm going to use Mockito.
First create a Factory with mocked instance:
public static interface GreetingService {
public String getGreeting(String name);
}
public static class MockGreetingServiceFactory
implements Factory<GreetingService> {
#Override
public GreetingService provide() {
final GreetingService mockedService
= Mockito.mock(GreetingService.class);
Mockito.when(mockedService.getGreeting(Mockito.anyString()))
.thenAnswer(new Answer<String>() {
#Override
public String answer(InvocationOnMock invocation)
throws Throwable {
String name = (String)invocation.getArguments()[0];
return "Hello " + name;
}
});
return mockedService;
}
#Override
public void dispose(GreetingService t) {}
}
Then use the AbstractBinder to bind the factory to the interface/service class, and register the binder. (It's all described in the link above):
#Override
public Application configure() {
AbstractBinder binder = new AbstractBinder() {
#Override
protected void configure() {
bindFactory(MockGreetingServiceFactory.class)
.to(GreetingService.class);
}
};
ResourceConfig config = new ResourceConfig(GreetingResource.class);
config.register(binder);
return config;
}
Seems like a lot, but it's just an option. I'm not too familiar with the test framework, or if it has an mocking capabilities for injection.
Here is the full test:
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
public class ServiceMockingTest extends JerseyTest {
#Path("/greeting")
public static class GreetingResource {
#Inject
private GreetingService greetingService;
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getGreeting(#QueryParam("name") String name) {
return greetingService.getGreeting(name);
}
}
public static interface GreetingService {
public String getGreeting(String name);
}
public static class MockGreetingServiceFactory
implements Factory<GreetingService> {
#Override
public GreetingService provide() {
final GreetingService mockedService
= Mockito.mock(GreetingService.class);
Mockito.when(mockedService.getGreeting(Mockito.anyString()))
.thenAnswer(new Answer<String>() {
#Override
public String answer(InvocationOnMock invocation)
throws Throwable {
String name = (String)invocation.getArguments()[0];
return "Hello " + name;
}
});
return mockedService;
}
#Override
public void dispose(GreetingService t) {}
}
#Override
public Application configure() {
AbstractBinder binder = new AbstractBinder() {
#Override
protected void configure() {
bindFactory(MockGreetingServiceFactory.class)
.to(GreetingService.class);
}
};
ResourceConfig config = new ResourceConfig(GreetingResource.class);
config.register(binder);
return config;
}
#Test
public void testMockedGreetingService() {
Client client = ClientBuilder.newClient();
Response response = client.target("http://localhost:9998/greeting")
.queryParam("name", "peeskillet")
.request(MediaType.TEXT_PLAIN).get();
Assert.assertEquals(200, response.getStatus());
String msg = response.readEntity(String.class);
Assert.assertEquals("Hello peeskillet", msg);
System.out.println("Message: " + msg);
response.close();
client.close();
}
}
Dependencies for this test:
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.0</version>
</dependency>
UPDATE
So in most cases, you really don't need a Factory. You can simply bind the mock instance with its contract:
#Mock
private Service service;
#Override
public ResourceConfig configure() {
MockitoAnnotations.initMocks(this);
return new ResourceConfig()
.register(MyResource.class)
.register(new AbstractBinder() {
#Override
protected configure() {
bind(service).to(Service.class);
}
});
}
#Test
public void test() {
when(service.getSomething()).thenReturn("Something");
// test
}
Much simpler!
Here is how I did it with Jersey 2.20, Spring 4.1.4 RELEASE, Mockito 1.10.8, and TestNG 6.8.8.
#Test
public class CasesResourceTest extends JerseyTestNg.ContainerPerMethodTest {
#Mock
private CaseService caseService;
#Mock
private CaseConverter caseConverter;
#Mock
private CaseRepository caseRepository;
private CasesResource casesResource;
#Override
protected Application configure() {
MockitoAnnotations.initMocks(this);
casesResource = new CasesResource();
AbstractBinder binder = new AbstractBinder() {
#Override
protected void configure() {
bindFactory(new InstanceFactory<CaseConverter>(caseConverter)).to(CaseConverter.class);
bindFactory(new InstanceFactory<CaseService>(caseService)).to(CaseService.class);
}
};
return new ResourceConfig()
.register(binder)
.register(casesResource)
.property("contextConfigLocation", "solve-scm-rest/test-context.xml");
}
public void getAllCases() throws Exception {
when(caseService.getAll()).thenReturn(Lists.newArrayList(new solve.scm.domain.Case()));
when(caseConverter.convertToApi(any(solve.scm.domain.Case.class))).thenReturn(new Case());
Collection<Case> cases = target("/cases").request().get(new GenericType<Collection<Case>>(){});
verify(caseService, times(1)).getAll();
verify(caseConverter, times(1)).convertToApi(any(solve.scm.domain.Case.class));
assertThat(cases).hasSize(1);
}
}
You also need this class which makes the binding code above a bit easier:
public class InstanceFactory<T> implements Factory<T> {
private T instance;
public InstanceFactory(T instance) {
this.instance = instance;
}
#Override
public void dispose(T t) {
}
#Override
public T provide() {
return instance;
}
}
Edited as pr. request. This is the contents of my test-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
It turns out that my test-context.xml does not instantiate any beans nor scan any packages, in fact, it does not do anything at all. I guess I just put it there in case I might need it.