Spring Boot integration

Find out how to integrate JSQL with Spring Boot

Stack requirements

The JSQL Spring Boot library will not work without an account in the JSQL cloud and a backend application written in Spring Boot. To learn more see overview.

Your backend application should have property configured database connection using JPA. For more details see Spring JPA integration.

Installation

Add JSQL artifactory into your maven repositories in your project.

For Maven setup:

<repositories>
    <repository>
        <id>maven</id>
        <url>http://46.41.135.99:8081/artifactory/libs-release-local/</url>
    </repository>
</repositories>

For Gradle setup:

repositories {

    mavenCentral()
    maven{
        url "http://46.41.135.99:8081/artifactory/libs-release-local"
    }

}

Then add jsql-spring-plugin dependency:

Releases list

For Maven setup:

<dependency>
    <groupId>it.jsql.connector.spring</groupId>
    <artifactId>jsql-spring-plugin</artifactId>
    <version>1.0.0.</version>
</dependency>

For Gradle setup:

compile(group: 'it.jsql.connector.spring', name: 'jsql-spring-plugin', version: '1.0.0')

 

Integration

The JSQL plugin for Spring Boot has the same integration principle as the Java EE or Spring 5 plugin. All you need to do is create a controller that will inherit from JSQLController and initiate JSQLService.

Example REST controller inheriting JSQLController:

import it.jsql.connector.controller.JSQLController;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExampleController extends JSQLController {

}

Now we have to implement the getJsqlService method and return the JSQLService instance from it:

@Override
public IJSQLService getJsqlService() {
    return new JSQLService(entityManager, API_KEY, DEV_KEY);
}

To the JSQLService constructor we need to pass the EntityManager, API_KEY and DEV_KEY.

The EntityManager we can get using @PersistanceContext annotation:

@PersistenceContext
EntityManager entityManager;

The API_KEY and DEV_KEY we can get from JSQL Admin Panel, see FAQ for details.

We can save them as protected final fields or store in file:

protected final String API_KEY = "==iSqF8rKvVeSgqudKDOXpjiFgGMJh1PbeouIz9IW/YQ==9CI8ox66gogpoSXm6yr";
protected final String DEV_KEY = "Z6kEovODxAv2I5hKekMyUw==";

Let’s show whole REST controller class implementation:

import it.jsql.connector.controller.JSQLController;
import it.jsql.connector.service.IJSQLService;
import it.jsql.connector.service.JSQLService;
import org.springframework.web.bind.annotation.RestController;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@RestController
public class ExampleController extends JSQLController {

 protected final String API_KEY = "==iSqF8rKvVeSgqudKDOXpjiFgGMJ1PbeouIzIW/YQ==9CI8ox66gogpoSXm6yr";
 protected final String DEV_KEY = "Z6kEovOiAv2I5hKekMyUw==";

 @PersistenceContext
 EntityManager entityManager;

 @Override
 public IJSQLService getJsqlService() {
  return new JSQLService(entityManager, API_KEY, DEV_KEY);
 }

}

Usage

If your application is integrated using the JSQL plugin, you can start it and try to call the query using CURL:

curl 'http://localhost:8080/select' -H 'Accept: application/json' -H 'Content-Type: application/json' 
--data-binary '{"token":"select * from person","params":{}}' --compressed

What next?

Close Menu