Reputation: 427
I'm experiencing a little issue that is wasting a lot of my time...
I've created, for demonstration purposes, a simple SpringBoot application using the Eclipse New > Spring Starter Project.
Here is my Application class:
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);
}
}
Then I've created the EditorsApis Controller:
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);
}
}
And an AuthorsApis and a BooksApis controllers that are very similar to the EditorApis one.
Of course I've created too all the three Pojos: Editor.class, Person.class and Book.class
I've started up the Eclipse embedded Spring runtime and I can see that all the paths are properly mapped:
INFO [main] s.w.s.m.m.a.RequestMappingHandlerMapping Mapped " {[/authors/],methods=[GET]}" onto public java.util.HashMap it.asirchia.apis.AuthorsApis.getAllAuthors()
And so on and so forth for all the other Rest APIs I've implemented. The last three lines of the log are:
Starting beans in phase 0
Tomcat started on port(s): 8080 (http)
Started Application in 5.547 seconds (JVM running for 6.169)
Ok, for me wverything is properly configured, up and running. But when I try to invoke
GET /authors HTTP/1.1
Host: localhost:8080
I obtain:
{
"timestamp": 1507286437765,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/authors"
}
And the same happens for ALL the REST APIs I've implemented. Any idea about the reason of this problem? Thank you.
Upvotes: 2
Views: 1592
Reputation: 3953
The following mapping will work localhost:8080/authors/ for you.Since in your method mapping GET you have added the "/" so you should provide the trailing slash in URL also. If you want mapping like this localhost:8080/authors then follow the below code,
@RequestMapping(value={"","/"}, method=RequestMethod.GET)
public HashMap<Long, Editor> getAllEditor(){
return Application.editors;
}
The above will accept,
1) localhost:8080/editors
2) localhost:8080/editors/
Hope this will help.
Upvotes: 3
Reputation: 2421
Can you just try to add a action content in value.Here only specifying only a "/".Like
@RequestMapping(value="/updateEditor", method=RequestMethod.GET)
If you need to add any path variable,you can modify the same with following,
@RequestMapping(value="/updateEditor/{editorid}", method=RequestMethod.PUT)
Just try this method also.
Upvotes: -2