Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Annotations your way in RESTEasy

DZone's Guide to

Annotations your way in RESTEasy

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}