I’m trying to access a simple api using POSTMAN but every time it give the error 404 not found. What can I do to access my api?
JavaScript
x
package api;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.BloggApplication;
import com.google.gson.Gson;
import model.User;
@RestController
public class UserApi {
Gson gson = new Gson();
@RequestMapping(value="api/createUser/",
method = RequestMethod.POST)
public int createUser(@RequestBody String json) {
User user = gson.fromJson(json, User.class);
BloggApplication.users.add(user);
return user.getId();
}
}
This is my PostApi class,
My main class is in BloggApplication.java.
I call the API using POSTMAN as this,
JavaScript
http://localhost:8080/api/createUser/
with body
JavaScript
{"Content-Type" :"application/json",
"userName" : "ali",
"password" : "123456"}
Advertisement
Answer
I solved the issue I moved Application class which contains the “springboot application” one level above other classes.
JavaScript
src/main/java/application.java
src/main/java/app/other classes