Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: how to handle incoming request to wrong context path

How do we handle incoming request to a wrong contextpath in spring mvc?

I have deployed a spring mvc application having contextpath as

http://exampledomain.com/mycontext

But when I try accessing url http://exampledomain.com/wrongcontext I get error as HTTP Status 404 - /wrongcontext/

I have implemented error handling which works fine for all wrong url when correct context path is used but it does not work for wrong context path.

I am trying to understand how do we redirect all incoming request to specific page in production environment..

thanks in advance

like image 502
Rajiv Kumar Avatar asked May 31 '26 12:05

Rajiv Kumar


2 Answers

Your application can only see requests to its context /mycontext There is nothing your application can do about requests to other contexts.

You can however deploy an application at the root context / and implement an error handler there.

Check out this answer: How to customize JBoss AS7 404 page

That answer relates to JBoss, but the same idea will apply on other servers.

like image 111
mad_fox Avatar answered Jun 04 '26 11:06

mad_fox


It is not possible to handle wrong context path requests in Spring since it only will handle the requests which goes to your context root. You have to take a look at server configuration parameters to redirect those kind of requests. If you are working on Tomcat, check path parameter of context.xml: https://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Defining_a_context

like image 33
okante Avatar answered Jun 04 '26 11:06

okante



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!