From 18b56311e5715fa72b58c3b696adf3fb339a9422 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 25 Jun 2017 19:23:25 +0300 Subject: [PATCH] HttpMediaTypeNotAcceptableException (#2145) * code snippets for the `Java 9 Stream API improvements` article * code snippets for the `Java 9 Stream API improvements` article [2 attempt] * removed the first attempt * the Spring 5 WebClient * delted stream features test * HttpMediaTypeNotAcceptableExceptionExampleController [0] * reactive web client service was removed --- ...tAcceptableExceptionExampleController.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spring-mvc-java/src/main/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionExampleController.java diff --git a/spring-mvc-java/src/main/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionExampleController.java b/spring-mvc-java/src/main/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionExampleController.java new file mode 100644 index 0000000000..539a6032a6 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionExampleController.java @@ -0,0 +1,27 @@ +package com.baeldung.exception; + +import org.springframework.http.MediaType; +import org.springframework.web.HttpMediaTypeNotAcceptableException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Collections; +import java.util.Map; + +@RestController +public class HttpMediaTypeNotAcceptableExceptionExampleController { + + @PostMapping(value = "/test", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public Map test() { + return Collections.singletonMap("key", "value"); + } + + @ResponseBody + @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) + public String handleHttpMediaTypeNotAcceptableException() { + return "acceptable MIME type:" + MediaType.APPLICATION_JSON_VALUE; + } + +}