简体   繁体   中英

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"}

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

this was the solution for me as well. Had to move the file containing

public static void main(String[] args) {
        SpringApplication.run(Dad1Application.class, args);
    }

to a higher level than the other files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM