A SpringBoot Developer’s Guide To Micronaut

Introduction

Creating a project

Creating a bean

  • @controller — To define your controller class for your rest endpoints.
  • @repository — To define your repository bean.
  • @singleton — To define a bean with singleton scope.
  • @prototype — To define a bean with prototype scope
  • @requestscope — To define a bean with request scopes.

Dependencies

   <dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.data</groupId>
<artifactId>micronaut-data-hibernate-jpa</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.sql</groupId>
<artifactId>micronaut-jdbc-hikari</artifactId>
<scope>compile</scope>
</dependency>

JPA configuration

@Entity
@Table(name = "Orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
datasources:
default:
url: jdbc:mysql://localhost:3306/ORDER
username: root
password: root
jpa:
default:
properties:
hibernate:
hbm2ddl:
auto: update
show_sql: true
dialect: org.hibernate.dialect.MySQL8Dialect
@Repository
public interface OrderRepository extends CrudRepository<Order, Long> {

@Query("select o from Order as o")
List<Order> getAllOrders();
}

REST Controller

@Controller("/order")
public class WebController {

private final OrderService orderService;

public WebController(OrderService orderService) {
this.orderService = orderService;
}

@Get("/{id}")
public HttpResponse<OrderDTO> getOrder(@PathVariable("id") Long id) {

Optional<OrderDTO> mayBeOrder = orderService.getOrder(id);
if (mayBeOrder.isPresent()) {
return HttpResponse.created(mayBeOrder.get());
}
return HttpResponse.notFound();
}

Performance

__  __ _                                  _   
| \/ (_) ___ _ __ ___ _ __ __ _ _ _| |_
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| | | | | (__| | | (_) | | | | (_| | |_| | |_
|_| |_|_|\___|_| \___/|_| |_|\__,_|\__,_|\__|
Micronaut (v2.5.8)
12:55:07.769 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
12:55:08.150 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
12:55:08.157 [main] INFO org.hibernate.Version - HHH000412: Hibernate ORM core version [WORKING]
12:55:08.248 [main] INFO o.h.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
12:55:08.351 [main] INFO org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQL57Dialect
12:55:09.059 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 1928ms. Server Running: http://localhost:8080

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store