Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need some clarity on Rest API

Posted on 2016-07-31
5
Medium Priority
?
101 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
gurpsbassi earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

721 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