Close

Spring MVC - RequestMappingHandlerMapping Examples

[Last Updated: Nov 8, 2025]

Spring MVC 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Controller
public class ExampleController {

@Autowired
private RequestMappingHandlerMapping mapping;

@RequestMapping("/example")
@ResponseBody
public String handle () {
System.out.println(mapping);
return "got it";
}
}




@Configuration
@ComponentScan
@EnableWebMvc
public class AppConfig implements WebMvcConfigurer {

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
UrlPathHelper urlPathHelper = new UrlPathHelper();
urlPathHelper.setAlwaysUseFullPath(true);
configurer.setUrlPathHelper(urlPathHelper);
}
}
Original Post




@Configuration
@ComponentScan
public class AppConfig extends WebMvcConfigurationSupport {

@Bean
@Override
public RequestMappingHandlerMapping requestMappingHandlerMapping(
@Qualifier("mvcContentNegotiationManager")
ContentNegotiationManager contentNegotiationManager,
@Qualifier("mvcApiVersionStrategy") @Nullable
ApiVersionStrategy apiVersionStrategy,
@Qualifier("mvcConversionService")
FormattingConversionService conversionService,
@Qualifier("mvcResourceUrlProvider")
ResourceUrlProvider resourceUrlProvider) {

// Fetch the default instance constructed by Spring
RequestMappingHandlerMapping m = super.requestMappingHandlerMapping(
contentNegotiationManager,
apiVersionStrategy,
conversionService,
resourceUrlProvider
);
m.setDefaultHandler(new MyDefaultHandler());
return m;
}
}
Original Post




See Also

Join