samedi 25 avril 2015

Java + Jersey + Gradle - java.sql.SQLException: No suitable driver found


Having trouble getting SQL working in a Java WebApp that I'm working on. It seems to be an issue with jersey not loading my sql driver.

Getting a java.sql.SQLException: No suitable driver found for jdbc:h2:testDbName when I call my endpoint.

Note that:

  1. the connection string is valid and works when not being run in the WebContainer.

  2. gradle script is configured to correctly place sqldriver (h2 in this case) in lib directory

I've simplified the code to the following:

TestResource.java

@Path("/user")
public class TestResource {

    @POST
    @Path("/add")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Response createForm(@FormParam("name") String name) throws Exception{

        Connection connection = DriverManager.getConnection("jdbc:h2:testDbName");
        connection.close();

        return Response.status(SC_ACCEPTED).build();
    }
}

build.gradle

apply plugin: 'war'

repositories {
    mavenCentral()
}

dependencies {
    compile 'javax.servlet:servlet-api:2.5'
    compile "javax.ws.rs:jsr311-api:1.1.1"
    compile 'com.sun.jersey:jersey-bundle:1.19'

    compile 'com.h2database:h2:1.3.175'
}

webapp.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://ift.tt/ra1lAU" xmlns="http://ift.tt/nSRXKP" xmlns:web="http://ift.tt/LU8AHS" xsi:schemaLocation="http://ift.tt/nSRXKP http://ift.tt/LU8AHS" id="WebApp_ID" version="2.5">

    <servlet>
        <servlet-name>Jersey</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>testPackage</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>Jersey</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

</web-app>


Aucun commentaire:

Enregistrer un commentaire