我正在创建一个 spring-rest-app。
这是我的调度程序配置(我也有一个具有 DataSource bean 的根配置)
@Configuration
@ComponentScan(basePackages= {"config", "cache", "dao", "entity", "exception", "rest", "service"})
@EnableWebMvc
public class DispatcherConfiguration {
@Bean
public KeyCache keyCache() {
return new KeyCacheImpl();
}
}
这是我的 webapp 初始化程序
public class TinyURLKeyServiceInitializor implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext appcontext = new AnnotationConfigWebApplicationContext();
appcontext.register(ConfigurationClass.class);
servletContext.addListener(new ContextLoaderListener(appcontext));
AnnotationConfigWebApplicationContext dispatchercontext = new AnnotationConfigWebApplicationContext();
dispatchercontext.register(DispatcherConfiguration.class);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(dispatchercontext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
这是控制器
@Controller
@RequestMapping("/api/keyservice")
public class KeyServiceController {
@Autowired
private KeyService keyService;
@GetMapping(value="/key", produces="application/json")
@ResponseBody
public String getKey() {
return keyService.getKey();
}
}
当我启动 Web 应用程序并发送 - GET http://localhost:7080/api/keyservice/key 我在调试日志中得到以下信息
[INFO] Completed initialization in 1006 ms
May 17, 2020 11:56:10 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-7080"]
[DEBUG] GET "/api/keyservice/key", parameters={}
[WARNING] No mapping for GET /api/keyservice/key
[DEBUG] Completed 404 NOT_FOUND
我已将 @EnableMvc 用于注册 MappingHandlers。但是他们仍然无法检测端点和控制器方法之间的映射。
我在 DispatcherServlet.getHandler 中放置了一个调试点,它每次都返回 null。有没有人遇到过类似的问题?