Spring Boot Return 204 No-Content When List Is Empty

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



Leave a reply



Submit