Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

Need some clarity on Rest API

Hi,
I am making a simple TODO app. where user can enter there email and a list of items to do.
I am planning to expose the following API (using spring mvc for this) :
 @RequestMapping(value = "/todo", method = RequestMethod.POST)
    @ResponseBody
    public APIPOSTResponse createTodo(@RequestBody User user) {
        if (user != null) {
            return userService.addUser(user);
        }
        return new APIPOSTResponse(false, "Couldn't add user at this time");
    }

    @RequestMapping(value = "/todo/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView viewTodo(@PathVariable int id, HttpServletRequest request) {
        User user = userService.getUser(id);
        ModelAndView mav = new ModelAndView("view");
        mav.addObject("name", user.getUserName());
        mav.addObject("email", user.getEmail());
        mav.addObject("todo", user.getTodo());
        return mav;
    }
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView emptyGet() {
        return new ModelAndView("new");

    }

Open in new window


Currently the above code only does todo creation and viewing
so initially i land on localhost:8080 where i enter my todo items.
on clicking on create the data goes into database. and from ajax there is a call to localhost/todo/ POST request to save the data into database.
there is a unique id assigned to the user. and then the for viewing there is a GET request to localhost/todo/1 (where 1 is the id here i am assuming)

Other things i am planning to add is the following requests  :
1) localhost/todo/1/edit  GET request to provide user with a page to edit the TODO list items
2) localhost/todo/1  PUT request to update the todo items

So in all there are following api calls  :
1) GET localhost:8080/
2) POST localhost:8080/todo
3) GET localhost:8080/todo/1
4) GET localhost:8080/todo/1/edit
5) PUT localhost:8080/todo/1


As per my understanding this is in compiliance with REST API and thats what REST API means.
Please correct my understanding

Thanks
ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rohit Bajaj

ASKER

HI,
Actually editing the resource is done by the call :
5) PUT localhost:8080/todo/1
This : GET localhost:8080/todo/1/edit  just opens a page UI where user can modify the items and then click on save on doing that the PUT request will be done.

So i think Editing the resource and getting the resource  have the same path.
what you think ?
I would use the same URL to get the resource for viewing and editing.

The PUT request is correct.
In that case what will be the http method for the two .. same url same methods ? what will differentiate that one is for viewing and one for editing ?
Same URL. Why would you need to differentiate? From a rest api perspective you should not differentiate. It's your application that needs to differentiate.

URLs in the application do no necessarily have to map the the same rest api URLs.

Your application can still browse to /edit, but it will still make the same get request.