the blog for developers

Adding Web Beans JSR 299 to Jersey for REST

While playing with Web Beans I thought it would be nice to add Web Beans support to Jersey. Jersey is a JSR 311 implementation for RESTful web services in Java. Though it has taken some flak, I – and others – think it’s easy to use. Because it’s easy in Jersey to control the creation of objects by writing your own servlet with a Jersey ComponentProvider, I finished a quick hack in no time. Some help was the integration examples for Spring.

public class WebbeansJerseyServlet extends ServletContainer {

  private static class WebbeansComponentProvider
    implements ComponentProvider {

    private WebBeansContainer webBeans = WebBeansContainer.create();

    public Object getInstance(ComponentProvider.Scope scope, Class c)
      throws InstantiationException, IllegalAccessException {
      return webBeans.getObject(c);
    }

    public Object getInstance(Scope scope, Constructor contructor,
                              Object[] parameters)
      throws InstantiationException, IllegalArgumentException,
      IllegalAccessException, InvocationTargetException {
      return null;
    }

    public void inject(Object instance) { }
  }

  protected void initiate(ResourceConfig rc, WebApplication wa) {
    wa.initiate(rc, new WebbeansComponentProvider());
  }
}

Having defined this servlet, a REST resource example for the UuidService from my last post about Web Beans, looks like this

@Path("/uuid")
public class UuidResource {
  @Named("uuid")
  UUIDService service;

  public UuidResource() {
  }

  @GET
  @ProduceMime("text/plain")
  public String getUuuid() {
    return service.getValue();
  }
}

Works like a charm. The UUIDService is injected through Web Beans and the scopes from Web Beans, @RequestScope and @SessionScope seem to work. This takes a big burden from the developer.

I hope to implement a SOFEA web application with Javascript parts which communicate with the backend via REST. For performance reasons it would be nice to populate and render the Javascript on the server. Therefore I would wish I could resolve REST calls to Jersey internally like

jersey.get("/helloworld/3");

to pre-populate pages with REST calls in them.

Thanks for listening.

Update:The Jersey lead wrote about a Jersey client API which perhaps does what I want. Splendid.

You can leave a Reply here. Of course, you should follow me on twitter here.

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

About the author: Stephan Schmidt is head of development at brands4friends. He has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.
Leave a reply.

Comments

Nice post.
But the link to client api is broken.

stephan

@Matthias: Thanks. And fixed.

Very nice, i will forward this to the Jersey users list [1].

The client API is very much work in progress, any suggestions/improvements are most welcome!

Paul.

[1] https://jersey.dev.java.net/servlets/SummarizeList?listName=users

Leave a Reply

What people wrote somewhere else:

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

NoSQL Guy

NoSQL: The Dawn of Polyglot Persistence

The dark side of NoSQL

Essential storage tradeoff: Simple Reads vs. Simple Writes

Sharding destroys the goals of your relational database

The unholy legacy of databases

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?