Solved

Need some clarity on Rest API

Posted on 2016-07-31
5
92 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 16

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 16

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 16

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

685 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