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; + } + +}