Spring-MVC 3.1: How to match URLs with trailing slash?

I am converting an obsolete servlet application in Spring 3.1. As a result, some URLs are out of date. We had problems with our network, which will not be resolved in the near future. My boss does not want to believe that their forwarding will always work. So she asked me to make my own redirects to webapp.

Everything works fine, except that if the URL has a Spring 3.1 trailing slash, the controller class function that processes it will not be found.

http: //blah.blah.blah/acme/makedonation found, displayed and processed

http: //blah.blah.blah/acme/makedonation / not

Here is the controller class that I use to handle my legacy URLs

import org.springframework.stereotype.Controller; import org.springframework.validation.*; import org.springframework.ui.ModelMap; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.SessionAttributes; import org.apache.log4j.Logger; @Controller public class LegacyServletController { private static final Logger logger = Logger.getLogger(LegacyServletController.class); // Redirect these legacy screns "home", the login screen via the logout process @RequestMapping({"makeadonation","contact","complain"}) public String home() { logger.debug("started..."); return "redirect:logout"; }// end home() }// end class LegacyServletController 

I figured out and found this post stack overflow offering some suggestions, but I'm new to Spring and don't understand it enough to implement some of these suggestions. This, in particular, looks like it will fit my needs well:

spring 3.1. RequestMappingHandlerMapping allows you to set the useTrailingSlashMatch property. This is true by default. I think switching to a false solution to your problem,

Will someone give me a basic example of how to do this, point me to a URL that has such an example (I had no luck with Google) or point me to a better idea?

Thanks a lot Steve

+6
source share
2 answers

you must configure your bean in context.xml and set the property. or you can refer to the link or spring section of doc 16.4

configuration example

 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <property name="useTrailingSlashMatch" value="true"> </property> </bean> 
+6
source

If you use spring Java @Configuration , you can also simply declare @Bean as follows:

 @Bean public RequestMappingHandlerMapping useTrailingSlash() { return new RequestMappingHandlerMapping() {{ setUseTrailingSlashMatch(true); }}; } 
+2
source

Source: https://habr.com/ru/post/920885/


All Articles