简体   繁体   中英

REST web application: what is the role of XML/JSON?

I am currently learning about REST applications, and particularly Java implementations of REST.

I am unsure of what role JSON or XML plays in in REST?

An example to show my current understanding:

  1. User clicks a button on front end.
  2. User is re-directed to a URL eg /user/{userid}
  3. Java method in service class calls repository (eg Crud Repository) class to retrieve data
  4. Repository pulls data from db (eg about that specific user)
  5. data passed back to service class and is then shown on the UI to user.

Where does JSON or XML fit into this process?

数据从服务返回到前端的格式。

The transmission of data between the front end and the api is done in JSON and/or XML .

So, simplisticly... the user asks for some data, through some web page, and the web page asks the RESTful API for the specific data, the api sends the web page the data as JSON , then the web page manipulates that and displays it or does whatever it needs to do with that data.

That is a general way to describe its role

A Method inside the controller is shown below which give json response

@RequestMapping(value = "/getSomething", method = RequestMethod.POST)
    public @ResponseBody String getSomething(HttpServletRequest req) 
    {

        JSONArray jsonArr = new JSONArray();
        Collection someList = new ArrayList();

        someList = someService.getsomeList(req); // here you get the response from service class to controller
        Iterator iter = categoryList.iterator();

        while (iter.hasNext()) // iterate the colleection
        {
                JSONObject jsonObj = new JSONObject();
                SomeClass someObj = (SomeClass) iter.next();
                jsonObj.put("name", someObj.getName());
                jsonArr.put(jsonObj);
        }

        return jsonArr.toString(); // return jsonstring as response
    }

This is how it can be processed in view (Say JSP ). Here an ajax call made to controller and response set to the field in the page.

$.ajax({
                        url : "getSomething.htm", //request send to controller
                        type : "POST",
                        data : {
                            'someData' : data
                        },
                        success : function(data) {
                            var arr = JSON.parse(data);
                            response($.map(arr, function(item) {
                                return {
                                    value : item.name, //setting the value to the view 

                                };
                            }));
                        }
                    });

Hope this helps!

If we divide your 5th step... 1) data is returned from service in a certain format 2) UI receives it in that format and display it on the screen. This format is XML or JSON or even plain text. This is the Accept type you mention when making a call from UI, and set the response header in service.

JSON stands for Javascript Object notation, hence if the response is in JSON format, you can directly use it as a javascript variable by just parsing it using JSON.parse. The Accept type is actually depends on your requirement. For most of the cases JSON is preferred, as it is easily converted to JS object.

As other have said, in your example JSON/XML fits at the end of the chain (almost), when data is returned from the server to the client.

Think of JSON and XML (and other formats) as a type of box. Some boxes are made of plastic, some are made of cardboard since they serve different purposes.

JSON/XML are types of "boxes" for the data you are sending/requesting to/from the server.

In your example:

  • User clicks a button on front end.
  • User is re-directed to a URL eg /user/{userid}
  • Java method in service class calls repository (eg Crud Repository) class to retrieve data
  • Repository pulls data from db (eg about that specific user)
  • Data from DB is "translated" from the server's data format ("type of box") into a more common format (like JSON or XML)
  • Data in JSON/XML format is sent to the front-end for displaying purposes

Think of it this way: if there wasn't a common format for people/systems to refer to, then if you query an Oracle database you would need to be able to understand that format. If you then had to query a Sybase database, then you would need to be able to understand that as well.

To solve this, you can pick a "common" format (a box that it's easier to fit in more trucks, as opposed to a box that can only be transported by a specific type of truck) and thus make it easier to "transport" that data around.

So in essence, JSON/XML is just a way of representing data, and as such it can be used to represent data that was originally in other "hard to read" format and make it easier to read or work with.

For example by returning your DB objects as JSON you can display them in a web page because most web page frameworks understand JSON as opposed to very few of them natively understanding Oracle/Sybase formats (there are drivers to do this but it is beyond the scope of this toy example).

You can also use it to communicate with other servers that also understand JSON (like third-party APIs for example) and thus if servers A and B have different data representations (internally one is Oracle backed and the other is Sybase backed) they can still talk to each other using a "common" format like JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM