没有找到GET apikeyservicekey的映射。

问题描述 投票:0回答:1

我正在创建一个spring-rest-app,这是我的dispatcher配置(我也有根配置,有DataSource bean)。

这是我的dispatcher配置(我还有一个有DataSource bean的root配置)。

@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();
    }
}

当我启动网络应用并发送 - GET http:/localhost:7080apikeyservicekey。我在DEBUG LOG中得到以下信息

[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。

我在DispatcherServlet.getHandler中放了一个调试点,但每次都返回null。有人遇到过类似的问题吗?

spring rest spring-rest eclipse-jee
1个回答
0
投票

找到了我打的原因--------。

http:/localhost:7080apikeyservicekey。

我的pom.xml有-

<plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>7080</port>
                <path>/api/keyservice</path>
            </configuration>
        </plugin>

我的控制器

@Controller
@RequestMapping("/api/keyservice")
public class KeyServiceController {

    @Autowired
    private KeyService keyService;

    @GetMapping(value="/key", produces="application/json")
    @ResponseBody
    public String getKey() {

由于context path = apikeyservice (由于pom.xml中的设置),spring试图为key找到一个映射。显然在我的控制器中没有key的映射。

删除了控制器的RequestMapping。然后就成功了。

© www.soinside.com 2019 - 2024. All rights reserved.