In this post, I am going to explain how to validate incoming data with Micronaut controllers using @Validated advice.
Define model class.
@Introspected
public class EmployeeRequest {
@Min(18)
@Max(62)
private Integer age;
@NotBlank
private String name;
.......
.......
}
Define controller class and annotate the controller method argument with @Valid annotation.
@Controller("/employees")
public class EmployeeController {
private static final AtomicInteger ID_COUNTER = new AtomicInteger(1);
@Post
@Consumes(MediaType.APPLICATION_JSON)
public HttpResponse saveEmployee(@Valid final EmployeeRequest employeeRequest) {
.......
.......
}
}
Find the below working application.
Step 1: Create new maven project ‘micronaut-validate-request-payload’.
Step 2: Update pom.xml with maven dependencies.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.app</groupId>
<artifactId>micronaut-validate-request-payload</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<parent>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-parent</artifactId>
<version>3.7.4</version>
</parent>
<properties>
<packaging>jar</packaging>
<jdk.version>11</jdk.version>
<release.version>11</release.version>
<micronaut.version>3.7.3</micronaut.version>
<micronaut.runtime>netty</micronaut.runtime>
<exec.mainClass>com.sample.app.App</exec.mainClass>
</properties>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-inject</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-validation</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-client</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-server-netty</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-jackson-databind</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micronaut.test</groupId>
<artifactId>micronaut-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.micronaut.build</groupId>
<artifactId>micronaut-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Uncomment to enable incremental compilation -->
<!-- <useIncrementalCompilation>false</useIncrementalCompilation> -->
<annotationProcessorPaths
combine.children="append">
<path>
<groupId>io.micronaut</groupId>
<artifactId>micronaut-http-validation</artifactId>
<version>${micronaut.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 3: Define EmployeeRequest class.
EmployeeRequest.java
package com.sample.app.dto;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import io.micronaut.core.annotation.Introspected;
@Introspected
public class EmployeeRequest {
@Min(18)
@Max(62)
private Integer age;
@NotBlank
private String name;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Step 4: Define EmployeeController class.
EmployeeController.java
package com.sample.app.controller;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.validation.Valid;
import com.sample.app.dto.EmployeeRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
@Controller("/employees")
public class EmployeeController {
private static final AtomicInteger ID_COUNTER = new AtomicInteger(1);
@Post
@Consumes(MediaType.APPLICATION_JSON)
public HttpResponse<Map<String, Object>> saveEmployee(@Valid final EmployeeRequest employeeRequest) {
final Map<String, Object> response = new HashMap<>();
response.put("id", ID_COUNTER.getAndIncrement());
response.put("name", employeeRequest.getName());
response.put("age", employeeRequest.getAge());
return HttpResponse.status(HttpStatus.CREATED).body(response);
}
}
Step 5: Define main application class.
App.java
package com.sample.app;
import io.micronaut.runtime.Micronaut;
public class App {
public static void main(String[] args) {
Micronaut.run(App.class);
// Use this if you want the beans to be initialized eagerly
/*
* Micronaut.build(args) .eagerInitSingletons(true) .mainClass(App.class)
* .start();
*/
}
}
Total project structure looks like below.
Build the project using mvn package command.
Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.
Upon command successful execution, you can see the jar file ‘micronaut-validate-request-payload-0.1.jar’ in project target folder.
$ ls ./target/
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-validate-request-payload-0.1.jar
original-micronaut-validate-request-payload-0.1.jar
test-classes
Execute below command to run the application.
java -jar ./target/micronaut-validate-request-payload-0.1.jar
Execute below command to run the application.
curl --location --request POST 'http://localhost:8080/employees' \
--header 'Content-Type: application/json' \
--data-raw '{
"age": 1,
"name": "Krishna"
}'
$curl --location --request POST 'http://localhost:8080/employees' \
> --header 'Content-Type: application/json' \
> --data-raw '{
> "age": 1,
> "name": "Krishna"
> }'
{"message":"Bad Request","_links":{"self":{"href":"/employees","templated":false}},"_embedded":{"errors":[{"message":"employeeRequest.age: must be greater than or equal to 18"}]}}
No comments:
Post a Comment