Platinum Partner
java

Annotations your way in RESTEasy

I've been thinking about how to allow you to process Annotations "your way" in RESTEasy. As a user base matures, it asks for more functionality, and allowing that extension via the Open/Closed Principal is the way to go. I added a feature in RESTEasy that will allow you to accomplish some of this.

Here's an example of how I used that flexibility to inject a Spring request scoped bean into a RESTEasy controlled "Resource" method. The end result looks like this:

  @Path("/")
public class TestBeanResource{
@GET
public String test(@Qualifier("testBean") TestBean bean){
return bean.configured;
}
}

Some complex scenarios that I worked on required complex rich-domain request-scoped beans. I had to do some whacky stuff in my business logic to make that happen. I really wanted a cleaner separation of concerns, and kept on wanting this kind of flexibility. Jersey (which I use in my day job) didn't have this kind of flexibility (since it very well might be an academic pursuit). I'm not sure how to build it there, so I tried it in RESTEasy, and it worked well from a proof-of-concept perspective.

In order to facilitate that behavior I needed to add the following bean:

@Provider
public static class QualifierInjectorFactoryImpl extends InjectorFactoryImpl implements
BeanFactoryAware {
BeanFactory beanFactory;

public QualifierInjectorFactoryImpl(ResteasyProviderFactory factory) {
super(factory);
}

public ValueInjector createParameterExtractor(Class injectTargetClass,
AccessibleObject injectTarget, Class type, Type genericType, Annotation[] annotations) {
final Qualifier qualifier = FindAnnotation.findAnnotation(annotations, Qualifier.class);
if (qualifier == null) {
return super.createParameterExtractor(injectTargetClass, injectTarget, type,
genericType, annotations);
} else {
return new ValueInjector() {
public Object inject(HttpRequest request, HttpResponse response) {
return beanFactory.getBean(qualifier.value());
}

public Object inject() {
// do nothing.
return null;
}
};
}
}

public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
}

Look for Spring's @Qualifier and inject a bean. It really doesn't look all that painful to me, but beauty is in the eye of the beholder.

What do you think? Here's the full TestCase and here's the Spring Configuration file

From http://www.jroller.com/Solomon/entry/annotations_your_way_in_resteasy

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}