Skip to content
Advertisement

Accessing Spring RestApi using POSTMAN (404 not found)

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?

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,

http://localhost:8080/api/createUser/

with body

{"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.

src/main/java/application.java
src/main/java/app/other classes
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement