Sunday 16 July 2023

Micronaut: bind path variables, headers and query parameters to a POJO

Using @RequestBean, we can bind all the bindable values like HttpRequest, @PathVariable, @QueryValue and @Header to a single POJO.

 

Example

@Get(value = "/{entity-name}{?country,state}", produces = MediaType.APPLICATION_JSON)
public Map<String, String> demo(@Valid @RequestBean DemoBean demoBean) { }

 

@Introspected
public class DemoBean {

	private HttpRequest<?> httpRequest;

	@PathVariable("entity-name")
	private String entityName;

	@Nullable
	@QueryValue
	@NotBlank
	private String country;

	@Nullable
	@QueryValue
	@NotBlank
	private String state;

	public DemoBean(HttpRequest<?> httpRequest, String entityName, @NotBlank String country, @NotBlank String state) {
		this.httpRequest = httpRequest;
		this.entityName = entityName;
		this.country = country;
		this.state = state;
	}
	.....
	.....
}

 

Find the below working application.

 

The bean class has to be introspected with @Introspected. It can be one of:

 

a.   Mutable Bean class with setters and getters

b.   Immutable Bean class with getters and an all-argument constructor (or @Creator annotation on a constructor or static method). Arguments of the constructor must match field names so the object can be instantiated without reflection.

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-request-bean-demo’.

 

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-request-bean-demo</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 DemoBean class.

 

DemoBean.java

 

package com.sample.app.model;

import javax.validation.constraints.NotBlank;

import io.micronaut.core.annotation.Introspected;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.annotation.PathVariable;
import io.micronaut.http.annotation.QueryValue;

@Introspected
public class DemoBean {

	private HttpRequest<?> httpRequest;

	@PathVariable("entity-name")
	private String entityName;

	@Nullable
	@QueryValue
	@NotBlank
	private String country;

	@Nullable
	@QueryValue
	@NotBlank
	private String state;

	public DemoBean(HttpRequest<?> httpRequest, String entityName, @NotBlank String country, @NotBlank String state) {
		this.httpRequest = httpRequest;
		this.entityName = entityName;
		this.country = country;
		this.state = state;
	}

	public HttpRequest<?> getHttpRequest() {
		return httpRequest;
	}

	public String getEntityName() {
		return entityName;
	}

	public String getCountry() {
		return country;
	}

	public String getState() {
		return state;
	}

}

 

Step 4: Define HelloController class.

 

HelloController.java

 

package com.sample.app.controller;

import java.util.*;

import javax.validation.Valid;

import com.sample.app.model.DemoBean;

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.RequestBean;

@Controller("/hello")
public class HelloController {

	@Get(value = "/{entity-name}{?country,state}", produces = MediaType.APPLICATION_JSON)
	public Map<String, String> demo(@Valid @RequestBean DemoBean demoBean) {
		
		Map<String, String> map = new HashMap<>();
		map.put("entityName", demoBean.getEntityName());
		map.put("country", demoBean.getCountry());
		map.put("state", demoBean.getState());
		
		return map;
	}

}

 

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-request-bean-demo-0.1.jar’ in project target folder.

 

$ls ./target/
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-request-bean-demo-0.1.jar
original-micronaut-request-bean-demo-0.1.jar
test-classes

 

Execute below command to run the application.

java -jar ./target/micronaut-request-bean-demo-0.1.jar

 

Run below command to test the api.

curl --location --request GET 'http://localhost:8080/hello/employees?country=india&state=karnataka'
$curl --location --request GET 'http://localhost:8080/hello/employees?country=india&state=karnataka'
{"country":"india","entityName":"employees","state":"karnataka"}

 

You can download this application from this link.


 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment