Search code examples
restspring-bootjerseyjax-rsresteasy

How to integrate a JAX-RS REST-Service with a JNDI lookup into SpringBoot?


I have a simple jax-rs REST-service that is deployed as a WAR on a wildfly server and uses a JNDI lookup for a datasource configured in the standalone.xml. For this the path is read from a datasource.properties file. The service then performas database actions through this datasource.

Now I want to use this REST-service in a SpringBoot application which is deployed to an embedded tomcat. My implementation uses RESTEasy and the service can easily be integrated with the resteasy-spring-boot-starter. But the JNDI lookup doesn't work, because of course the datasource is now not configured in a standalone.xml, but in the application.properties file. It is a completely different datasource.

I'm looking for a solution to set the datasource without having to "hard code" it. This is how the connection is retrieved currently in the WAR for the wildfly:

private Connection getConnection() {

    Connection connection = null;

    try (InputStream config = OutboxRestServiceJbossImpl.class.getClassLoader().getResourceAsStream("application.properties")) {

        Properties properties = new Properties();
        properties.load(config);
        DataSource ds = (DataSource) new InitialContext().lookup(properties.getProperty("datasource"));
        connection = ds.getConnection();

    } catch (Exception e) {

    }

    return connection;
}

Currently I solved this by having a core module which actually performs the logic and 2 implementations with jax-rs for wildfly and SpringMVC in SpringBoot. They invoke the methods of an instance of the core module and the the connection is handed over to these methods. This looks like this for wildfly:

public String getHelloWorld() {


    RestServiceCoreImpl rsc = new RestServiceCoreImpl();

    try (Connection connection = getConnection()) {

        String helloWorld  = rsc.getHelloWorld(connection);

    } catch (Exception e) {

    }
    return helloWorld;
}


public String getHelloWorld(Connection connection){

//database stuff, eg. connection.execute(SQL);
}

And like this in SpringBoot:

@Autowired
RestServiceCoreImpl rsc;

@Autowired
DataSource restServiceDataSource;

@Override
public String getHelloWorld() {

    try (Connection connection = restServiceDataSource.getConnection()){
        return rsc.getHelloWorld(connection);

    } catch (SQLException e) {

    }

    return null;
}

Is there any way to solve this datasource issue? I need the SpringMVC solution to be replaced with the jax-rs solution within SpringBoot.


Solution

  • Okay, I was able to solve this myself. Here is my solution:

    I enabled the naming in the embedded tomcat server as follows:

    @Bean
    public TomcatServletWebServerFactory tomcatFactory() {
    return new TomcatServletWebServerFactory() {
        @Override
        protected TomcatWebServer getTomcatWebServer(org.apache.catalina.startup.Tomcat tomcat) {
            tomcat.enableNaming(); 
            return super.getTomcatWebServer(tomcat);
        }
    

    Then I was able to add the JNDI ressource in the server context. Now a JNDI lookup is possible.