为什么我的春季启动@RequestMapping不起作用?

use*_*525 1 java spring spring-mvc spring-boot

我有以下内容:

@RepositoryRestController
public class DataSetController {

     @RequestMapping(value = "/cars/test", method = RequestMethod.GET)
        public String testFetch() {
         return "HELLO";

     }
}

@RepositoryRestResource
public interface DataSetRepository extends PagingAndSortingRepository<DataSet, Integer>, QueryDslPredicateExecutor<DataSet> {}
Run Code Online (Sandbox Code Playgroud)

启动时的日志表明以下内容:

2015-08-21 18:49:46.0​​50 INFO 52448 --- [main] osdrwRepositoryRestHandlerMapping:将"{[cars/test],methods = [GET]}"映射到公共java.lang.String com.example.hello上. dataset.DataSetController.testFetch()

我的配置中的基础uri是:

base-uri:/ api

所以我应该能够获得localhost:8080/api/cars/test

但这是我在日志中得到的:

2015-08-21 18:58:10.847 WARN 52476 --- [nio-8080-exec-1] osweb.servlet.PageNotFound:在带有名称的DispatcherServlet中找不到带有URI [/ api/cars/test]的HTTP请求的映射'DispatcherServlet的'

为什么?

afr*_*sse 5

您的基本URI似乎/api未在配置中正确设置,这就是调度程序无法找到正确映射的原因.

由于您的所有请求都有/api基础,我建议您在Controller中添加@RequestMapping注释:

@RepositoryRestController
@RequestMapping("/api")
public class DataSetController {

}
Run Code Online (Sandbox Code Playgroud)