spring 动态设置 RequestMapping
原理:自定义 HandlerMapping ,把请求都集中到一个controller中
定义自己的 HandlerMapping
package com.example.demo; import javax.servlet.http.HttpServletRequest; import org.springframework.web.servlet.handler.AbstractHandlerMapping; /** * @author siqi */ public class MyHandlerMapping extends AbstractHandlerMapping { public WelcomeController welcomeController; @Override protected Object getHandlerInternal(HttpServletRequest request) throws Exception { return welcomeController; } }
定义controller
package com.example.demo; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class WelcomeController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("simple url======"); response.getWriter().write("hello"); return null; } }
添加到容器中
@Bean public WelcomeController simpleUrlController() { return new WelcomeController(); } @Bean public MyHandlerMapping myHandlerMapping() { MyHandlerMapping myHandlerMapping = new MyHandlerMapping(); //注意优先级 myHandlerMapping.setOrder(Integer.MAX_VALUE - 10); myHandlerMapping.welcomeController = simpleUrlController(); return myHandlerMapping; }
完成
BeanNameUrlHandlerMapping
根据bean的名字即为url地址,需要以"/"开头,支持通配符
@Configuration public class BeanNameUrlHandlerMappingConfig { @Bean BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() { return new BeanNameUrlHandlerMapping(); } @Bean("/beanNameUrl") public WelcomeController welcome() { return new WelcomeController(); } }
可以在xml中配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> /simpleurl=simpleUrlController </value> </property> <property name="order" value="1"/> </bean> <bean id="simpleUrlController" class="com.example.demo.WelcomeController"/> </beans>
package com.example.demo; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class WelcomeController extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("simple url======"); response.getWriter().write("hello"); return null; } }
SimpleUrlHandlerMapping
更加灵活,可以在url和bean之间随意指定
@Configuration public class SimpleUrlHandlerMappingConfig { @Bean public SimpleUrlHandlerMapping simpleUrlHandlerMapping() { SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping(); Map<String, Object> urlMap = new HashMap<>(); urlMap.put("/simpleUrlWelcome", welcome()); simpleUrlHandlerMapping.setUrlMap(urlMap); return simpleUrlHandlerMapping; } @Bean public WelcomeController welcome() { return new WelcomeController(); } }
同等xml
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> /simpleUrlWelcome=welcome /*/simpleUrlWelcome=welcome </value> </property> </bean> <bean id="welcome" class="com.baeldung.WelcomeController" />
或
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/simpleUrlWelcome">welcome</prop> <prop key="/*/simpleUrlWelcome">welcome</prop> </props> </property> </bean>
WelcomePageHandlerMapping
默认页面的映射,在静态资源路径下 找 index.html 文件
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false) public class ResourceProperties { private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" }; /** * Locations of static resources. Defaults to classpath:[/META-INF/resources/, * /resources/, /static/, /public/]. */ private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;
WebMvcAutoConfiguration.java
@Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) { WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping( new TemplateAvailabilityProviders(applicationContext), applicationContext, getWelcomePage(), this.mvcProperties.getStaticPathPattern()); welcomePageHandlerMapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider)); welcomePageHandlerMapping.setCorsConfigurations(getCorsConfigurations()); return welcomePageHandlerMapping; }
参考:https://www.baeldung.com/spring-handler-mappings