Spring boot return 204 No-Content when List is empty
I researched a little more and found this, it's closer to what I was looking for, with this I don't need to check if the list is empty to return
Return HTTP 204 on null with spring @RestController
Of course yes.
Option 1 :
@RestController
public class RepoController {
@RequestMapping(value = "/document/{id}", method = RequestMethod.GET)
public Object getDocument(@PathVariable long id, HttpServletResponse response) {
Object object = getObject();
if( null == object ){
response.setStatus( HttpStatus.SC_NO_CONTENT);
}
return object ;
}
}
Option 2 :
@RestController
public class RepoController {
@RequestMapping(value = "/document/{id}", method = RequestMethod.GET)
public Object getDocument(@PathVariable long id) {
Object object = getObject();
if ( null == object ){
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
return object ;
}
}
Might have typos, but you get the concept.
Is this the correct way to return 204 No Content using Spring?
Comparing to 200 response your 204 response has no Content-Length
header. Try adding it manually to see if it helps with your application server handling empty response body:
return ResponseEntity.noContent().header("Content-Length", "0").build();
How to return 204 for Null list or objective instead of 200 []
You can use ResponseEntity model please see the following example:
@GetMapping(path = "/getall")
public ResponseEntity<List<User>> findAll() {
List<User> users = repository.findAll();
if (CollectionUtils.isEmpty(users)){
return ResponseEntity.noContent()
.build();
}
return ResponseEntity.ok()
.body(users);
}
Spring MVC - Automatically return 204 when rest controller response type is void
I was able to implement a solution based on the link provided by Yoshua. I'm not 100% sure on the implementation details, but it's working for me.
This captures the response from all of the controller methods that have a return type of void
and changes their HTTP Status to 204.
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
@ControllerAdvice
public class NoContentControllerAdvice implements ResponseBodyAdvice<Void> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
if(returnType.getParameterType().isAssignableFrom(void.class)) {
return true;
}
return false;
}
@Override
public Void beforeBodyWrite(Void body, MethodParameter returnType, MediaType mediaType,
Class<? extends HttpMessageConverter<?>> converterType, ServerHttpRequest request, ServerHttpResponse response) {
if(returnType.getParameterType().isAssignableFrom(void.class)) {
response.setStatusCode(HttpStatus.NO_CONTENT);
}
return body;
}
}
Related Topics
Spring Boot @Enablescheduling Conditionally
Setting Custom Key When Pushing New Data to Firebase Database
How to Junit Test That Two List<E> Contain the Same Elements in the Same Order
Passing Empty List as Parameter to JPA Query Throws Error
How to Apply Spring Boot Filter Based on Url Pattern
How to Update a Jsf Component from a Jsf Backing Bean Method
How to Select One Object from the List of Objects
How to Split Single Row into Multiple Rows in Spark Dataframe Using Java
Mock User Console Input in Junit Test
Calling Mutiple Webservice At Same Time in Spring Java
Sending Variable from Adapter to Activity
Best Way to Run Jar Application With Arguments from Env Variables
How to Find the Longest Word in a Given String
Set Drawable Size Programmatically
Spring MVC - Get Httpservletresponse Body
Way to Check If Two Collections Contain the Same Elements, Independent of Order
Use a Binary Search on an Int Array Sorted in Descending Order
How to Increment a Variable to the Next or Previous Letter in the Alphabet