1
votes

I am trying to use a datasource declared in my JBoss EAP 7 standalone.xml:

<datasource jndi-name="java:jboss/datasources/my_db" pool-name="my_db" ...>
...
</datasource>

By the way I haven't found any doc about the name conventions, would java:/datasources/my_db be correct too?

Then as soon as I add the following to my RESTEasy web.xml (no other modifications):

<context-param>
    <param-name>resteasy.jndi.resources</param-name>
    <param-value>java:jboss/datasources/my_db</param-value>
</context-param>

the application breaks with the error:

java.lang.RuntimeException: RESTEASY003130: Class is not a root resource.
It, or one of its interfaces must be annotated with @Path:
org.jboss.as.connector.subsystems.datasources.WildFlyDataSource implements:
javax.sql.DataSource java.io.Serializable

How to access JNDI datasources in RESTEasy?
Thanks.

2

2 Answers

0
votes

As you are running in a WildFly/JBossEAP 7 container you can completely forget that it's implementation of JAX-RS is built on RestEASY.

You can completely remove the web.xml (or at least any RestEASY config that it contains) and just build "pure" JAX-RS:

@Path("...")
public class SomeRestService {

    @Resource(name="java:jboss/datasources/my_db")
    private DataSource ds;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public MyPojo someResource() {

        try(Connection con = ds.getConnection()) {
            ...
        }

    }

}

You can inject EJBs, CDI beans, PersistenceContexts, etc. It will "just work".

0
votes

Steve's answer is correct but there is another way to do it:

DataSource ds = (DataSource) new InitialContext().lookup("java:jboss/datasources/my_db");

I prefer using @Resource