I am working on an application where I do not want my rest APIs to receive XML-type requests, and if they do receive such requests they should not be processed and it should directly be filtered in the config of spring boot. How can I achieve it?
Asked
Active
Viewed 144 times
0
-
That is what the `consumes` variable is for in `@RequestMapping`. – M. Deinum Sep 05 '22 at 12:16
-
@M.Deinum Yes true, but I want to disable it for whole application, That'd be for single request right ? – Rahul khanvani Sep 05 '22 at 12:20
-
what you are looking for is interceptors https://www.baeldung.com/spring-mvc-handlerinterceptor – Akash Jain Sep 05 '22 at 12:23
-
Afaik there isn't, however if there is no XML library in your app it won't process XML. If all else fails you can always write a simple filter or Spring `HandlerInterceptor` that checks the content-type/accept-header and disallow the request based on the content-type. – M. Deinum Sep 05 '22 at 12:25
-
Does this answer your question? [How to set the default content type in Spring MVC in no Accept header is provided?](https://stackoverflow.com/questions/18189245/how-to-set-the-default-content-type-in-spring-mvc-in-no-accept-header-is-provide) – star67 Sep 06 '22 at 06:43
1 Answers
0
I would suggest you try to set the default content type in WebConfigurer class and then handle the exception if there is incoming request that does not match the content type you expected via ControllerAdvice of your own implementation and extends it with ResponseEntityExceptionHandler then provide the handler function for HttpMediaTypeNotSupportedException or any other exception you want.

Aleson
- 332
- 2
- 9