【发布时间】:2017-08-07 22:32:44
【问题描述】:
我正在尝试实现 Springboot REST api。并且我定义 @RestController 的类似乎不起作用。我有一个名为 MyService 的类,它实现了所有抽象方法。我在类声明的顶部添加了@RestController 注释,并为我需要从其余调用调用的方法添加了@RequestMapping 注释。但这不起作用。我用一个没有实现任何接口并且工作正常的类尝试了这个。
这里是代码
package com.my.service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyService implements MyServiceInterface{
@Override
@RequestMapping("/age")
public String Age() {
return "24";
}
}
MyServiceInterface 的代码
public interface MyServiceInterface {
public String Age();
}
我从邮递员那里得到的错误是
{"timestamp":1489688505136,"status":404,"error":"Not Found","message":"No message available","path":"/age"}
【问题讨论】:
-
能否请您分享您的“MyServiceInterface”并分享您遇到的异常的堆栈跟踪......
-
@VelNaga 检查我的编辑
-
当你启动 spring-boot 应用程序时,它会打印控制台和/或日志中的所有映射。看看
/age是否映射到任何东西。您的问题可能出在配置中。在 Java 中,方法名称也以小写字母开头。 -
@jny 我检查了控制台和 /age 没有映射其他人被映射(他们的类没有实现任何接口的那个)
-
如果“/age”没有被映射意味着检查你的包结构.....这个包应该是你的主类的一个子包,或者在你的包中配置@ComponentScan。 ..只需分享您的包结构...
标签: rest spring-boot spring-restcontroller