5

In my spring boot application, I have a controller for post request which produces and consumes JSON request and I have defined @ExceptionHandler for this controller with HttpMediaTypeNotAcceptableException for catching HttpMediaTypeNotAcceptableException(406 status code) exceptions.

Now whenever I call the post request with a wrong header of ACCEPT such as application/pdf, the defined exception does not get called.

I don't want to define ControllerAdvice since I would like to throw a specific error for this controller.

code:

@RestController
@RequestMapping("/school")
public class SchoolCotroller {

    @Autowired
    private SchoolService schoolService;

    @PostMapping(produces = "Application/Json", consumes = "Application/Json")
    @ResponseStatus(HttpStatus.CREATED)
    public School postData(@Valid @RequestBody School school)  {

       return  schoolService.save(school);

    }
 @ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
    String handleMediaTypeNotAcceptable(
            final HttpMediaTypeNotAcceptableException exception,
            final NativeWebRequest request) {
        return "acceptable MIME type:" + MediaType.APPLICATION_JSON_VALUE;
        }
}

curl:

curl --location --request POST 'http://localhost:8080/school' \
--header 'Accept: application/pdf' \
--header 'Content-Type: application/json' \
--data-raw '{
    "studentName":"mayank",
    "course":1,
    "teacher":"e",
    "id":1
}' 

So whenever I call this Curl request I get default bad request response with console log as "DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]" but it does not call the ExceptionHandler method.

Why is it not calling the required ExceptionHandler method?

2 Answers 2

0

Maybe springboot is having trouble recognizing the consumes and produces type as application/json since you provided it as "Application/Json" at your endpoint. Try the following code :

@RestController
@RequestMapping("/school")
public class SchoolCotroller {

    @Autowired
    private SchoolService schoolService;

    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE , consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseStatus(HttpStatus.CREATED)
    public School postData(@Valid @RequestBody School school)  {

       return  schoolService.save(school);

    }

    @ExceptionHandler(HttpMediaTypeNotAcceptableException.class)
    @ResponseStatus(HttpStatus.NOT_ACCEPTABLE)
    String handleMediaTypeNotAcceptable(
            final HttpMediaTypeNotAcceptableException exception,
            final HttpServletRequest request) {
        return "acceptable MIME type:" + MediaType.APPLICATION_JSON_VALUE;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It still gives the same thing even after changing produces to "MediaType.APPLICATION_JSON_VALUE "
@MayankGoyal I have updated my answer could you please check again with this code.
tried with the above code but no luck. Another interested thing is that if I add @ExceptionHandler(Exception.class) instead of @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) then, also it does not invoke that method either.
You're missing an @ControllerAdvice
0

I faced similar issue. I added Content-Type header as MediaType.APPLICATION_JSON_VALUE before throwing the error as HttpStatus.NOT_ACCEPTABLE and it's working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.