From b3aae2e8224186eaf6b64f99ff7d883dfbfe02a4 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 24 Mar 2021 18:39:38 +0300 Subject: Handle HttpMediaTypeNotAcceptableException --- src/main/java/com/juick/www/controllers/Site.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 notFoundAction() { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } + + @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) + public ResponseEntity 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); + } } -- cgit v1.2.3