Injecting EJB into a JAX-RS

Following my previous post here, I tried to inject an EJB 3.1 stateless session bean into this resource, only to find the infamous java.lang.NullPointerException.

Supposed you have an EJB as follows:

MainService

package com.wordpress.dwuysan.service;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;

@Stateless
@LocalBean
public class MainService {

    public String sayHello(final String name) {
        return "Hello" + name;
    }
}

And injecting it in a JAX-RS bean:

GenericResource

package com.wordpress.dwuysan;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path(value = "generic")
public class GenericResource {

    @Inject
    private com.wordpress.dwuysan.service.MainService mainService;

    @GET
    public String get() {
        return this.mainService.sayHello("test");
    }
}

This will result in java.lang.NullPointerException.

To fix this, the JAX-RS bean must also be “managed”, so simply add the annotation javax.annotation.ManagedBean to your JAX-RS bean (Nelson 2011)

References:

Nelson, L, 2011, ‘How do I inject a local stateless session bean into a JAX-RS resource using CDI?’, accessed 06 December 2012.

Advertisements

Starting with JAX-RS: Configuring javax.ws.rs.ApplicationPath

I recently started with a very simple Hello-World-like JAX-RS. So, to start with, I created a class like this:

GenericResource

package com.wordpress.dwuysan;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path(value = "generic")
public class GenericResource {

    @GET
    public String get() {
        return "Hello World";
    }
}

Once the application server is running, when I run curl http://<web application address>/generic, it failed.

After a little bit of Google-ing around, I found that you need to set up the resource path, either by installing a correct servlet on web.xml, or if you are fortunate enough and are using Java EE 6, you can use @javax.ws.rs.ApplicationPath and extends javax.ws.rs.core.Application (Kuchtiak 2009).

So, once I add this class:

ApplicationConfig

package com.wordpress.dwuysan;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath(value = "resources")
public class ApplicationConfig extends Application {
}

I can now invoke curl http://<web application address>/resources/generic, I got correct response.

References:

Kuchtiak, M, 2009, 'How to Combine REST Services with EJB 3.1', accessed 06 December 2012.

Netbeans 7.3 connecting to local mysql

I thought I’ll write this one up since I just spent 30 minutes trying to connect to local mysql.

I just downloaded the new Netbeans 7.3 Beta 2, and am now trying to play around with MySql. So, firstly, I simply use the Synaptic Package Manager and install mysql. Once that’s done, it’s time to connect.

I have used Oracle in the past and have managed to connect to it via Netbeans Services > Database (and downloading the ojdbc drivers, connecting, etc etc, they all goes without saying). With MySql however, I notice, there is a dedicated menu Register MySql Server.

Ooooooooooooooohhhhhhhhhhh…… fantastic!!!

Okay …

Then comes the page this this MySQL Server Properties. Hmmm, what values should I put in? …

Well, I spent some time looking up google and also trying various entries. I thought I’ll just share the screenshot here:

mysqlConfig