Reputation: 516
My spring controller should take any get request of any URI type except a URI which started with /resources/*
as this is the call, if I am not wrong, done by spring container to load all the static content in view page.
Current configuration is like this
@RequestMapping(method = RequestMethod.GET, value="/*")
which working fine for request with URI /
or /example
but if someone make a typo in URI eg. /example/random/char
it is throwing exception trace page not found(404) which is pretty obvious and leads me to make value="/**"
@RequestMapping(method = RequestMethod.GET, value="/**")
then the request with URI /
or /example
are unable to load my static content .js, .css or .png
files in my view page. This lead me to the conclusion that spring internally make get call to load these files with an URL pattern eg. http://localhost:8080/resources/my.jpg
.
Note:- I intentionally removed my project name by configuring tomcat.
Update Okay @dimitrisli comment make me realize that I need to elaborate a little about my project.
It basically a URL redirecting component. A user simply call my application through get request and ended with redirecting to the actual page.
For example user request me throw their browser with URL let's say http://localhost:8080/sample
. Now my application needs to split the URL to get the string sample
and search in db for the actual URL against it(so sample
is the alias name for a really long URL). If URL exist it will redirect to the actual page else page does not exist message will show.
Upvotes: 1
Views: 3816
Reputation: 138
It sounds like you need add a resource handler to your project. This will allow files in your /resources
folder to pass through. Here is another post about it with some example code for reference.
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
Another option may be to implement an interceptor that catches all URIs before making it to your controller method. This would allow you to add some logic to make sure things exist in the DB prior to actually reaching your controller rather than having a single controller method doing everything. Extending HandlerInterceptorAdapter might be something to look into.
Upvotes: 0
Reputation: 21401
(Update based on comments)
This is how you can deal with 404 errors.
Option 1
in web.xml
include the following to forward on main page:
<error-page>
<error-code>404</error-code>
<location>/</location>
</error-page>
or your custom error specific page:
<error-page>
<error-code>404</error-code>
<location>/my-custom-error-page</location>
</error-page>
Option 2
Define a catch-all error handler at the Controller layer:
@ExceptionHandler(Exception.class)
public String handleAllExceptions(Exception e) {
log.error("There was an exception thrown! ", e);
return "index";
}
Based on the name of the folder you want to exclude (resources) and your description (.js/.css/.png) I'm assuming you are referring to the static content of your web project.
You need to explicitly define the static content in your dispatcher servlet xml
as follows (follow the directory convention of your project for static data):
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/css/**" location="/resources/css/" />
<mvc:resources mapping="/images/**" location="/resources/images/" />
<mvc:resources mapping="/js/**" location="/resources/js/" />
<mvc:resources mapping="/fonts/**" location="/resources/fonts/" />
Upvotes: 1