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

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.0modelVersion>
groupId>com.sample.appgroupId>
artifactId>void-controller-methodartifactId>
version>1version>

parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>2.5.6version>
relativePath />
parent>


dependencies>

dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>


dependencies>

build>
plugins>
plugin>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-maven-pluginartifactId>
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


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

Share the post

Spring rest: void controller method

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×