annotationProcessor platform("io.micronaut.platform:micronaut-platform:4.6.3")
implementation platform("io.micronaut.platform:micronaut-platform:4.6.3")
annotationProcessor("io.micronaut:micronaut-inject-java")
annotationProcessor("jakarta.annotation:jakarta.annotation-api")
implementation("jakarta.annotation:jakarta.annotation-api")
annotationProcessor("jakarta.persistence:jakarta.persistence-api")
implementation("jakarta.persistence:jakarta.persistence-api")
implementation("io.micronaut.spring:micronaut-spring-boot-starter")
Table of Contents
Micronaut Data from a Spring Boot Application
This guide shows how to use Micronaut Data from a Spring Boot application.
Authors: Sergio del Amo
Micronaut Version: 4.6.3
1. What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 17 or greater installed with
JAVA_HOME
configured appropriately
2. Introduction
In this guide, you will use Micronaut Data JDBC within a Spring Boot application.
3. Solution
We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.
-
Download and unzip the source
4. Generate a Spring Boot Application
Generate a Spring Boot application using Spring Initializr with Spring Web.
5. Dependencies
Add the following dependencies as described in Using the Micronaut Spring Boot Starter to use Micronaut Features within a Spring Boot application.
Moreover, add the following dependencies to use Micronaut Data:
annotationProcessor("io.micronaut.data:micronaut-data-processor")
implementation("io.micronaut.data:micronaut-data-jdbc")
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
runtimeOnly("com.h2database:h2")
6. Configuration
Add configuration for an in-memory H2 Database.
#Mon Nov 18 15:11:17 UTC 2024
datasources.default.password=
datasources.default.dialect=H2
datasources.default.driverClassName=org.h2.Driver
datasources.default.schema-generate=CREATE_DROP
datasources.default.url=jdbc\:h2\:mem\:devDb;LOCK_TIMEOUT\=10000;DB_CLOSE_ON_EXIT\=FALSE
datasources.default.username=sa
7. Mapped Entities with Java Records
Create a Micronaut Data Mapped Entity
package example.micronaut;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.data.annotation.GeneratedValue;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.MappedEntity;
@MappedEntity (1)
public record Book(
@Id (2)
@GeneratedValue (3)
@Nullable (4)
Long id,
String title,
int pages
) {
}
1 | Annotate the class with @MappedEntity to map the class to the table defined in the schema. |
2 | Specifies the ID of an entity |
3 | Specifies that the property value is generated by the database and not included in inserts |
4 | Annotate it with @Nullabe because it is a generated value. |
Learn how to leverage Java records for immutable configuration, Micronaut Data Mapped Entities and Projection DTOs in the Micronaut Data and Java Records guide. |
8. JDBC Repository
Next, create a repository interface to define the operations to access the database. Micronaut Data will implement the interface at compilation time.
package example.micronaut;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.CrudRepository;
@JdbcRepository(dialect = Dialect.H2) (1)
public interface BookRepository extends CrudRepository<Book, Long> { (2)
Book save(String title, int pages);
}
1 | @JdbcRepository with a specific dialect. |
2 | By extending CrudRepository you enable automatic generation of CRUD (Create, Read, Update, Delete) operations. |
9. Controller
Create a controller which exposes a GET route at /books
.
package example.micronaut;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
@RestController (1)
public class BookController {
@Autowired (2)
private BookRepository bookRepository;
@GetMapping("/books") (3)
Iterable<Book> list() {
return bookRepository.findAll();
}
}
1 | In Spring’s approach to building RESTful web services, HTTP requests are handled by a controller. These components are identified by the @RestController annotation. |
2 | Inject a bean of type BookRepository by using @Autowired on the field definition. |
3 | The @GetMapping ensures that HTTP GET requests to /books are mapped to the list method. |
10. Test
The following test shows that you can use Micronaut Data JDBC as your persistence solution in a Spring Boot Application.
package example.micronaut;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.web.util.UriComponentsBuilder;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) (1)
class BookControllerTest {
@LocalServerPort (2)
private int port;
@Autowired (3)
private TestRestTemplate restTemplate;
@Autowired (4)
BookRepository bookRepository;
@Test
void booksGet() {
assertEquals(0, booksJsonArray().length);
Book moreJava17 = bookRepository.save("More Java 17", 951);
Book[] books = booksJsonArray();
assertEquals(1, books.length);
assertNotNull(books[0].id());
assertEquals(books[0].pages(), 951);
assertEquals(books[0].title(), "More Java 17");
bookRepository.delete(moreJava17);
assertEquals(0, booksJsonArray().length);
}
private Book[] booksJsonArray() {
return restTemplate.getForObject(booksRequestUriString(), Book[].class);
}
private String booksRequestUriString() {
return UriComponentsBuilder.fromUriString("http://localhost:" + port)
.path("books")
.build()
.toUriString();
}
}
1 | The @SpringBootTest annotation tells Spring Boot to look for a main configuration class (one with @SpringBootApplication , for instance) and use that to start a Spring application context. |
2 | Note the use of webEnvironment=RANDOM_PORT to start the server with a random port (useful to avoid conflicts in test environments) and the injection of the port with @LocalServerPort . |
3 | Inject a bean of type TestRestTemplate by using @Autowired on the field definition. |
4 | Inject a bean of type BookRepository by using @Autowired on the field definition. |
11. Testing the Application
To run the tests:
./gradlew test
12. Next steps
Read more about Micronaut Spring and Micronaut Data.
13. Help with the Micronaut Framework
The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.
14. License
All guides are released with an Apache license 2.0 license for the code and a Creative Commons Attribution 4.0 license for the writing and media (images…). |