I have a spring boot application with a GET service.
@RequestMapping(value = "/abc/track/{type}", method = RequestMethod.GET)
public void DummFunc(
@RequestParam(value="subs", required = false) String sub,
, HttpServletResponse response) {}
value for subs is an encoded value.
If I pass following as value to parameter subs
{%22endpoint%22:%22https://def.abc.com/tyu/send/eD3vpGNQW28:APA91bHOo8rYrV0xccdQz3okjZJG-QGrJX8LG6ahJnEUpMNfGedqi3hJxQsJx_8BMbH6oDjaSPPEXqzNWchWrGSkhuTkSdemikkys1U22Ipd7MsRw0owWbw89V2fslIVAJ6G5lkyvYuQ%22,%22expirationTime%22:null,%22keys%22:{%22p256dh%22:%22BK0pktn50CMsTQtqwdPlKlJtdFs0LFeXX14T1zgoz6QWnvSTp5dxtChnUP5P1JX0TsjcopbdPKyN31HfABMUyic%22,%22auth%22:%22qbO_z0vGao9wD-E5g8VU-A%22}}
It fails to capture the request and control does not come inside of the function.
If we instead pass as value to parameter subs:
%7B%22endpoint%22:%22https://def.abc.com/tyu/send/dX5q5eV7hFQ:APA91bHib-0QXrMzjatcvTR_uaIeeJK8lf6GmXUC9Jxv0Oxth-BzD4GmWnd4-YpDZv8qSFZ0eSg9mB2YkRvkc5ezdXW5KeaHjuQZfdyDxyBXjJgE-25Xbtlk37pdm8vfLk20k0k_VxW9%22,%22expirationTime%22:null,%22keys%22:%7B%22p256dh%22:%22BCCvcBLpRqp4u0auP688_MUJLsAiwWlQSn5kpyl8YVsEo_J-KpSdnhCmVIE_BhDXBcFYflPK52hqhYf3EaOCyuY%22,%22auth%22:%22iKuW_ESkCZnubWcQu_JK8w%22%7D%7D
It works fine.
Basically, the request that has issue getting through has { and } instead of %7Band %7D.
My question is instead of application failing with 400 bad request ,how do I capture such requests in my app, encode them properly and then process them or may be atleast handle this error
Also, I tried adding Filter and RestControllerAdvice.
Findings so far:
- What I realised was that probably request does not even reach the application because If I remove
DummFuncfrom my code that is mapped to request/abc/track/{type}, It still does not return404but 400 only. CommonsLoggingFilteris not logging this particular request with 400 bad response code but is able to log rest of the requests (Another hint that request does not hit the application - I guess)- While debugging, I realised that if I hit this API from chrome app of
Postmanand browser, it returns 400 Bad request but if I hit this API from desktopPostmanapp, it returns 200. - I also tried adding exception-handler in the controller as well through a new class, none of them captured the request
// @ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(Throwable.class)
public void handle(HttpMessageNotReadableException e) {
System.out.println("EXCEPTION HAPPENED");
System.out.println(e);
}
In my application.properties, relevant props are as follows:
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
#security.user.name=user
#security.basic.enabled=true
logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG,stdout
log4j.logger.httpclient.wire=DEBUG,stdout
management.endpoints.web.exposure.include=*
management.endpoint.beans.cache.time-to-live=10s
server.tomcat.accesslog.enabled = true
log4j.logger.org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod=DEBUG,stdout
logging.level.org.springframework.web=DEBUG,stdout
server.tomcat.uri-encoding=UTF-8
So, what is it that can be done to handle it properly?
I am using IntelliJ and it's embedded Tomcat server.