2017-10-06 118 views
1

我遇到被浪费了我很多时间有点问题......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); 
    } 

} 

而一个AuthorsApisBooksApis控制器非常相似到EditorApis之一。

当然,我已经创建了太所有三个的POJO: Editor.classPerson.classBook.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的我实现了。 有关这个问题的原因的任何想法? 谢谢。

+0

试试localhost:8080/editors /或localhost:8080/authors /。它应该可以工作 – VelNaga

+0

我试过了...它不起作用 – sirnino

+0

对不起@VelNaga随着斜线的运行... 谢谢 – sirnino

回答

1

以下映射将为您工作localhost:8080/authors/。由于在您的方法映射GET中添加了“/”,因此您应该在URL中提供尾部斜线。如果你想这样本地主机映射:8080 /作者然后按照下面的代码,

@RequestMapping(value={"","/"}, method=RequestMethod.GET) 
public HashMap<Long, Editor> getAllEditor(){ 
    return Application.editors; 
} 

以上接受,

1)本地主机:8080 /编辑

2)本地主机: 8080 /编辑/

希望这会有所帮助。

-1

可你只是尝试添加在value.Here一个动作内容仅供只指定“/".Like

@RequestMapping(value="/updateEditor", method=RequestMethod.GET) 

如果您需要添加任何路径变量,你可以修改同与以下,

@RequestMapping(value="/updateEditor/{editorid}", method=RequestMethod.PUT) 

刚刚尝试这种方法也。