java 为什么我的 Spring Boot @RequestMapping 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32150452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:44:11  来源:igfitidea点击:

Why isn't my spring boot @RequestMapping working?

javaspringspring-mvcspring-boot

提问by user602525

I have the following:

我有以下几点:

@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> {}

The logs at startup indicate the following:

启动时的日志指示以下内容:

2015-08-21 18:49:46.050 INFO 52448 --- [ main] o.s.d.r.w.RepositoryRestHandlerMapping : Mapped "{[/cars/test],methods=[GET]}" onto public java.lang.String com.example.hello.dataset.DataSetController.testFetch()

2015-08-21 18:49:46.050 INFO 52448 --- [main] osdrwRepositoryRestHandlerMapping:映射到公共 java.lang.String com.example.hello 上的“{[/cars/test],methods=[GET]}”。数据集.DataSetController.testFetch()

My base uri in my config is:

我的配置中的基本 uri 是:

base-uri: /api

基 URI:/api

So I should be able to get localhost:8080/api/cars/test

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

But here is what I get in the logs:

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

2015-08-21 18:58:10.847 WARN 52476 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/api/cars/test] in DispatcherServlet with name 'dispatcherServlet'

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

Why?

为什么?

回答by afraisse

It seems that your base URI /apiis not set properly in your configuration, that's why the dispatcher can't find a proper mapping.

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

Since all your requests will have the /apibase I suggest you add a @RequestMapping annotation to your Controller :

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

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

}

回答by Andy Wilkinson

You've said that you've used base-uriin your config, but it should be spring.data.rest.base-uri.

你说过你已经base-uri在你的配置中使用过,但它应该是spring.data.rest.base-uri.