I've got following code for my REST Controller:
@RequestMapping(value = "foo", method = RequestMethod.GET)
public ResponseEntity<Result> doSomething(@RequestParam int someParam)
{
try
{
final Result result = service.getByParam(someParam);
if (result == null)
{
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} else
{
return ResponseEntity.ok(result);
}
} catch (Exception ex)
{
LOG.error("Error blah", ex);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
I would like to use ResponseEntity.noContent().build() but Eclipse gives me:
Type mismatch: cannot convert from ResponseEntity to ResponseEntity
Is there any way to overcome this?
Update:
It is possible to create helper like this:
public class ResponseUtils
{
public static <T> ResponseEntity<T> noContent()
{
return withStatus(HttpStatus.NO_CONTENT);
}
public static <T> ResponseEntity<T> internalServerError()
{
return withStatus(HttpStatus.INTERNAL_SERVER_ERROR);
}
public static <T> ResponseEntity<T> accepted()
{
return withStatus(HttpStatus.ACCEPTED);
}
private static <T> ResponseEntity<T> withStatus(HttpStatus status)
{
return new ResponseEntity<T>(status);
}
}
So I can use it like:
return ResponseUtils.noContent();
But maybe there is built-in functionality for this stuff?