我遇到被浪费了我很多时间有点问题......404上正确映射SpringBoot RestController
我已经创建,用于演示目的,使用Eclipse的新一个简单的SpringBoot应用>春入门项目。
这里是我的应用程序类:
package it.asirchia;
//All needed imports
@SpringBootApplication
public class Application {
public static HashMap<Long,Book> books = new HashMap<Long, Book>();
public static HashMap<Long,Editor> editors = new HashMap<Long, Editor>();
public static HashMap<Long,Person> authors = new HashMap<Long, Person>();
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
然后我创建了EditorsApis控制器:
package it.asirchia.apis;
//All needed imports
@RestController
@RequestMapping(value="/editors")
public class EditorsApis {
private static long counter = 0;
@RequestMapping(value="/", method=RequestMethod.GET)
public HashMap<Long, Editor> getAllEditor(){
return Application.editors;
}
@RequestMapping(value="/", method=RequestMethod.POST)
public void postNewEditor(@RequestBody Editor editor){
Application.editors.put(counter++, editor);
}
@RequestMapping(value="/{editorid}", method=RequestMethod.PUT)
public void updateEditor(@PathVariable long editorid,
@RequestBody Editor editor){
Application.editors.put(editorid, editor);
}
@RequestMapping(value="/{editorid}", method=RequestMethod.GET)
public Editor getEditor(@PathVariable long editorid){
return Application.editors.get(editorid);
}
@RequestMapping(value="/{editorid}", method=RequestMethod.DELETE)
public void deleteEditor(@PathVariable long editorid){
Application.editors.remove(editorid);
}
}
而一个AuthorsApis和BooksApis控制器非常相似到EditorApis之一。
当然,我已经创建了太所有三个的POJO: Editor.class,Person.class和Book.class
我已经开始了Eclipse的嵌入式Spring运行和我可以看到,所有的路径被正确映射:
INFO [主要] swsmmaRequestMappingHandlerMapping映射 “ {[/作者/],方法= [GET]}” 到公共的java.util.HashMap it.asirchia。 API s.AuthorsApis.getAllAuthors()
对于我实现的所有其他Rest API,依此类推。 最后三行日志是:
在阶段开始豆类0
Tomcat的开始端口:8080(HTTP)
在5.547秒(JVM运行启动的应用程序6.169)
好吧,对我来说,一切都已正确配置,正常运行。但是,当我尝试调用
GET /authors HTTP/1.1
Host: localhost:8080
我获得:
{
"timestamp": 1507286437765,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/authors"
}
而同样的情况对于ALL的REST API的我实现了。 有关这个问题的原因的任何想法? 谢谢。
试试localhost:8080/editors /或localhost:8080/authors /。它应该可以工作 – VelNaga
我试过了...它不起作用 – sirnino
对不起@VelNaga随着斜线的运行... 谢谢 – sirnino