Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Micronaut: support non-standard HTTP methods

@CustomHttpMethod annotation support non-standard http methods, that you can provide by specifying the required method property.

 

Examples

@CustomHttpMethod(method = "LOCK", value = "/lock-employee/{employeeId}")
public Employee lockEmployee(@NonNull @PathVariable(name = "employeeId") final Integer empId) {
	return employeeService.lockEmployee(empId);
}

@CustomHttpMethod(method = "UNLOCK", value = "/unlock-employee/{employeeId}")
public Employee unlockEmployee(@NonNull @PathVariable(name = "employeeId") final Integer empId) {
	return employeeService.unlockEmployee(empId);
}

 

@CustomHttpMethod annotation can be used anywhere the standard method annotations can be used, including controllers and declarative HTTP clients

 

Find the below working application.

 

Step 1: Create new maven project ‘micronaut-rest-non-standard-methods’.

 

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.0modelVersion>
	groupId>com.sample.appgroupId>
	artifactId>micronaut-rest-non-standard-methodsartifactId>
	version>0.1version>
	packaging>jarpackaging>

	parent>
		groupId>io.micronautgroupId>
		artifactId>micronaut-parentartifactId>
		version>3.7.4version>
	parent>

	properties>
		packaging>jarpackaging>
		jdk.version>11jdk.version>
		release.version>11release.version>
		micronaut.version>3.7.3micronaut.version>
		micronaut.runtime>nettymicronaut.runtime>
		exec.mainClass>com.sample.app.Appexec.mainClass>
	properties>

	repositories>
		repository>
			id>centralid>
			url>https://repo.maven.apache.org/maven2url>
		repository>
	repositories>

	dependencies>
		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-injectartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-validationartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-http-clientartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-http-server-nettyartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>io.micronautgroupId>
			artifactId>micronaut-jackson-databindartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>jakarta.annotationgroupId>
			artifactId>jakarta.annotation-apiartifactId>
			scope>compilescope>
		dependency>
		dependency>
			groupId>ch.qos.logbackgroupId>
			artifactId>logback-classicartifactId>
			scope>runtimescope>
		dependency>
		dependency>
			groupId>io.micronaut.testgroupId>
			artifactId>micronaut-test-junit5artifactId>
			scope>testscope>
		dependency>
		dependency>
			groupId>org.junit.jupitergroupId>
			artifactId>junit-jupiter-apiartifactId>
			scope>testscope>
		dependency>
		dependency>
			groupId>org.junit.jupitergroupId>
			artifactId>junit-jupiter-engineartifactId>
			scope>testscope>
		dependency>
	dependencies>

	build>
		plugins>
			plugin>
				groupId>io.micronaut.buildgroupId>
				artifactId>micronaut-maven-pluginartifactId>
			plugin>

			plugin>
				groupId>org.apache.maven.pluginsgroupId>
				artifactId>maven-compiler-pluginartifactId>
				configuration>
					
					

					annotationProcessorPaths
						combine.children="append">
						path>
							groupId>io.micronautgroupId>
							artifactId>micronaut-http-validationartifactId>
							version>${micronaut.version}version>
						path>
					annotationProcessorPaths>

				configuration>
			plugin>
		plugins>
	build>

project>

Step 3: Define Employee model class.

 

Employee.java

package com.sample.app.model;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;

import io.micronaut.core.annotation.NonNull;

public class Employee {

	@NonNull
	@Positive
	private Integer id;

	@NotBlank
	private String name;

	@NonNull
	@Positive
	private Integer age;

	private Boolean lock = false;

	public Employee() {
	}

	public Employee(Integer id, String name, Integer age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Boolean getLock() {
		return lock;
	}

	public void setLock(Boolean lock) {
		this.lock = lock;
	}

}

Step 4: Define EmployeeService class.

 

EmployeeService.java

package com.sample.app.service;

import java.util.ArrayList;
import java.util.List;

import com.sample.app.model.Employee;

import jakarta.inject.Singleton;

@Singleton
public class EmployeeService {
	private static final List emps = new ArrayList();
	private static final Employee EMP_NOT_FOUND = new Employee();

	private static int counter = 4;

	static {
		Employee emp1 = new Employee(1, "Sunil", 23);
		Employee emp2 = new Employee(2, "Shetty", 31);
		Employee emp3 = new Employee(3, "Ram", 43);
		Employee emp4 = new Employee(4, "Akansha", 21);

		emps.add(emp1);
		emps.add(emp2);
		emps.add(emp3);
		emps.add(emp4);

	}

	public Listall() {
		return emps;
	}

	public Employee add(final Employee emp) {
		emp.setId(counter);
		counter++;

		emps.add(emp);
		return emp;
	}

	public Employee byId(final Integer id) {
		for (Employee emp : all()) {
			if (id.equals(emp.getId())) {
				return emp;
			}
		}
		return EMP_NOT_FOUND;
	}

	public Employee lockEmployee(final Integer id) {
		Employee emp = byId(id);
		emp.setLock(true);
		return emp;
	}

	public Employee unlockEmployee(final Integer id) {
		Employee emp = byId(id);
		emp.setLock(false);
		return emp;
	}
}

Step 5: Define EmployeeController class.

 

EmployeeController.java

package com.sample.app.controller;

import com.sample.app.model.Employee;
import com.sample.app.service.EmployeeService;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.CustomHttpMethod;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.PathVariable;
import jakarta.inject.Inject;

@Controller("/employees")
public class EmployeeController {

	@Inject
	private EmployeeService employeeService;

	@CustomHttpMethod(method = "LOCK", value = "/lock-employee/{employeeId}")
	public Employee lockEmployee(@NonNull @PathVariable(name = "employeeId") final Integer empId) {
		return employeeService.lockEmployee(empId);
	}
	
	@CustomHttpMethod(method = "UNLOCK", value = "/unlock-employee/{employeeId}")
	public Employee unlockEmployee(@NonNull @PathVariable(name = "employeeId") final Integer empId) {
		return employeeService.unlockEmployee(empId);
	}

	@Get("/{employeeId}")
	public Employee byId(@NonNull @PathVariable(name = "employeeId") final Integer empId) {
		return employeeService.byId(empId);
	}

}

Step 6: 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-rest-non-standard-methods-0.1.jar’ in project target folder.

$ls ./target/
classes
generated-sources
generated-test-sources
maven-archiver
maven-status
micronaut-rest-non-standard-methods-0.1.jar
original-micronaut-rest-non-standard-methods-0.1.jar
surefire-reports
test-classes

Execute below command to run the application.


java -jar ./target/micronaut-rest-non-standard-methods-0.1.jar

 

Execute below commands to confirm the functionality.

curl --location --request GET 'http://localhost:8080/employees/1'
curl --location --request LOCK 'http://localhost:8080/employees/lock-employee/1'
curl --location --request GET 'http://localhost:8080/employees/1'
curl --location --request UNLOCK 'http://localhost:8080/employees/unlock-employee/1'
curl --location --request GET 'http://localhost:8080/employees/1'

$curl --location --request GET 'http://localhost:8080/employees/1'
{"id":1,"name":"Sunil","age


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Micronaut: support non-standard HTTP methods

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×