Solved

Need some clarity on Rest API

Posted on 2016-07-31
5
73 Views
Last Modified: 2016-08-13
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
0
Comment
Question by:Rohit Bajaj
  • 3
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
gurpsbassi earned 500 total points
ID: 41736435
I wouldn't do number 4.
Editing the resource and getting the resource should have the same path.

If your application needs to behave differently in edit mode, then simply return http allow methods in the http header.
0
 

Author Comment

by:Rohit Bajaj
ID: 41736645
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 ?
0
 
LVL 15

Expert Comment

by:gurpsbassi
ID: 41736666
I would use the same URL to get the resource for viewing and editing.

The PUT request is correct.
0
 

Author Comment

by:Rohit Bajaj
ID: 41736668
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 ?
0
 
LVL 15

Expert Comment

by:gurpsbassi
ID: 41736683
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.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now