diff options
author | Vitaly Takmazov | 2021-03-24 19:01:19 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2021-03-24 19:01:19 +0300 |
commit | b73568fe17500765c33f6a062226e6b433412c49 (patch) | |
tree | c9ccd89f97c551005fee3cdbb0eabc8af99a4906 /src/main/java/com/juick/www/api/Index.java | |
parent | b3aae2e8224186eaf6b64f99ff7d883dfbfe02a4 (diff) |
Cleanup HttpMediaTypeNotAcceptableException handler
* move ExceptionHandler to ControllerAdvice
* add test
* reindent using VSCode
Diffstat (limited to 'src/main/java/com/juick/www/api/Index.java')
-rw-r--r-- | src/main/java/com/juick/www/api/Index.java | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/src/main/java/com/juick/www/api/Index.java b/src/main/java/com/juick/www/api/Index.java index bafb6758..2d71683e 100644 --- a/src/main/java/com/juick/www/api/Index.java +++ b/src/main/java/com/juick/www/api/Index.java @@ -18,11 +18,18 @@ package com.juick.www.api; import io.swagger.v3.oas.annotations.Hidden; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.HttpMediaTypeNotAcceptableException; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import java.net.URI; @@ -30,13 +37,20 @@ import java.net.URI; /** * Created by vitalyster on 25.07.2016. */ +@ControllerAdvice @RestController public class Index { - + private static final Logger logger = LoggerFactory.getLogger("API"); @Hidden @RequestMapping(value = { "/api/", "/ws/" }, method = RequestMethod.GET) public ResponseEntity<Void> description() { URI redirectUri = ServletUriComponentsBuilder.fromCurrentRequestUri().path("/swagger-ui.html").build().toUri(); return ResponseEntity.status(HttpStatus.MOVED_PERMANENTLY).location(redirectUri).build(); } + @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) + public ResponseEntity<String> invalidMediaType(final HttpMediaTypeNotAcceptableException exception, + final NativeWebRequest request) { + logger.info("Media type not resolved ({}) for url: {}", request.getHeaderValues("Accept"), ServletUriComponentsBuilder.fromCurrentRequestUri()); + return ResponseEntity.badRequest().body("Invalid media type"); + } } |