Wednesday, 1 December 2021

Spring rest: void controller method

In this post, I am going to explain how to design void controller method. Void controller methods are useful, when you do not want to send any response body.

 

Just by returning the ResponseEntity with appropriate headers, we can address this usecase.

 

Example

@GetMapping(value = "/health")
@ResponseStatus(value = HttpStatus.OK)
public void updateDataThatDoesntRequireClientToBeNotified() {
	System.out.println("Request received to check application health");
}

 

Find the below working application.

 

Step 1: Create new maven project ‘void-controller-method’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

 

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>void-controller-method</artifactId>
	<version>1</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.6</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>


	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>


	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

 

Step 3: Define HelloController class.

 

HelloController.java

package com.sample.app.controller;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@GetMapping(value = "/health")
	@ResponseStatus(value = HttpStatus.OK)
	public void updateDataThatDoesntRequireClientToBeNotified() {
		System.out.println("Request received to check application health");
	}
}

 

Step 4: Define main application class.

 

App.java

package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}

}

 

Total project structure looks like below.



Run App.java.

 

Open the url ‘http://localhost:8080/health’ in browser, you can observe response code value as 200.

 



You can download complete working application from below link.

https://github.com/harikrishna553/springboot/tree/master/rest/void-controller-method


 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment