Solved

Need some clarity on Rest API

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 56
jboss wildfly 10.1 10 218
Java: anonymous class 4 29
Crystal Reports Licensing Questions 4 33
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

789 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