I'm trying to come up to speed on hk2 and creating a custom annotation for injection on a Resource method, so I leveraged the helloworld-spring-webapp as my base application (in a Spring environment, it seemed like a logical choice). I've followed several things found on the web on setting this up, most notably these 2 links:
Gist on github
https://gist.github.com/anonymous/389e1e8064174bdcd102
Link on SO
http://stackoverflow.com/questions/18463311/jersey-2-how-to-replace-injectableprovider-and-abstracthttpcontextinjectable
I am seeing the Resource get called TWICE for a single http request. I'm assuming I'm missing a configuration (or have done it incorrectly). Any help?
SampleParam.java
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER })
public @interface SampleParam {
}
SampleParamInjectionResolver.java
@Singleton
public class SampleInjectionResolver implements InjectionResolver<SampleParam> {
@Override
public Object resolve(Injectee injectee, ServiceHandle<?> root) {
return "THIS HAS BEEN INJECTED APPROPRIATELY";
}
@Override
public boolean isConstructorParameterIndicator() {
return false;
}
@Override
public boolean isMethodParameterIndicator() {
return true;
}
public static final class Binder extends AbstractBinder {
@Override
protected void configure() {
bind(SampleInjectionResolver.class).to(
new TypeLiteral<InjectionResolver<SampleParam>>() {
}).in(Singleton.class);
}
}
}
MyApplication.java
public class MyApplication extends ResourceConfig {
/**
* Register JAX-RS application components.
*/
public MyApplication () {
register(new SampleInjectionResolver.Binder());
register(RequestContextFilter.class);
register(JerseyResource.class);
register(SpringSingletonResource.class);
register(SpringRequestResource.class);
register(CustomExceptionMapper.class);
}
}
JerseyResource.java
@Path("jersey-hello")
public class JerseyResource {
private static final Logger LOGGER = Logger.getLogger(JerseyResource.class.getName());
// @Autowired
// private GreetingService greetingService;
// @Autowired
// @Inject
// private DateTimeService timeService;
public JerseyResource() {
LOGGER.fine("HelloWorldResource()");
}
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello(@SampleParam String inject) {
System.err.println("EXECUTING!");
System.err.println("*******************************INJECTED: " + inject);
return inject;
// return String.format("%s: %s", timeService.getDateTime(), greetingService.greet("world"));
}
}
Output from server for single http request:
EXECUTING!
*******************************INJECTED: THIS HAS BEEN INJECTED APPROPRIATELY
EXECUTING!
*******************************INJECTED:
And I get no content returned from the resource (injected is blank and is what is returned). Anyone have an idea as to what I'm doing wrong?
Thanks in advance,
Eric
________________________________________________________
The information contained in this e-mail is confidential and/or proprietary to Capital One and/or its affiliates. The information transmitted herewith is intended only for use by the individual or entity to which it is addressed. If the reader of this message is not the intended recipient, you are hereby notified that any review, retransmission, dissemination, distribution, copying or other use of, or taking of any action in reliance upon this information is strictly prohibited. If you have received this communication in error, please contact the sender and delete the material from your computer.