I'm trying to create a Spring Boot starter project that defines controllers programmatically based on the existence of certain types of beans. In the auto configuration class I defined a RequestMappingHandlerMapping
where I register the handlers programmatically similar to the following:
@Bean
public RequestMappingHandlerMapping customRoutes(ApplicationContext context) {
var handlerMapping = new RequestMappingHandlerMapping();
findTargetBeans(context).forEach((name, bean) ->
var getMapping = RequestMappingInfo
.paths("/" + name)
.methods(RequestMethod.GET)
.produces(MediaType.APPLICATION_JSON_VALUE)
.build();
handlerMapping.registerMapping(getMapping, createHandler(), getHanderMethod());
);
return handlerMapping;
}
This seems to build just fine, but when I try hitting the registered paths, I get a 404 response. Am I missing something? Do I need additional configuration to make this programmatic routing to work? I've been looking at old questions like this one, and this one, but none of the answers work for me.