diff options
author | Vitaly Takmazov | 2021-03-24 18:39:38 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2021-03-24 18:39:38 +0300 |
commit | b3aae2e8224186eaf6b64f99ff7d883dfbfe02a4 (patch) | |
tree | 949b5d01b53411172b4848146cf60842dff248f4 | |
parent | b236ca54772d9dcc76e8694012dab99044601b5d (diff) |
Handle HttpMediaTypeNotAcceptableException
-rw-r--r-- | src/main/java/com/juick/www/controllers/Site.java | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/src/main/java/com/juick/www/controllers/Site.java b/src/main/java/com/juick/www/controllers/Site.java index 1e308f6a..ed8f951e 100644 --- a/src/main/java/com/juick/www/controllers/Site.java +++ b/src/main/java/com/juick/www/controllers/Site.java @@ -30,16 +30,21 @@ import com.juick.util.MessageUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.text.StringEscapeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; +import org.springframework.web.HttpMediaTypeNotAcceptableException; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.context.request.NativeWebRequest; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; @@ -59,6 +64,7 @@ import java.util.stream.Collectors; */ @Controller public class Site { + private static final Logger logger = LoggerFactory.getLogger("Site"); @Inject private UserService userService; @Inject @@ -594,4 +600,11 @@ public class Site { public ResponseEntity<String> notFoundAction() { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } + + @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) + public ResponseEntity<String> invalidMediaType(final HttpMediaTypeNotAcceptableException exception, + final NativeWebRequest request) { + logger.info("Invalid media type {}, url: {}", request.getHeaderValues("Content-Type"), ServletUriComponentsBuilder.fromCurrentRequestUri()); + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } } |