• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Having some problem to understand how to manage JSON request

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello to everybody,

I am studing the Spring MVC showwcase dowloadable from the STS dashboard.

Now I am studing how Spring MVC mapping the resources and I have some problem to understand the following thing:

So, I have the following link that generate an HTTP Request towards the "/mapping/produces" folder:



As you can see this link have class named "writeJsonLink" and for this class there is defined the following JQuery function triggered on the click of the link:



Ok, I have commented the code to try to understand its behavior (I am a beginner of Javascript and JQuery) and seems me that the behavior of this script is the following one: When I click the link, start the function that before send the ajax call to the web application check if the url of the clicked link is an address that end with .json extension.

In this case this case my URL don't end with .json (because my url is towards the FOLDER: /mapping/produces and not towards a .json file), so the function don't add the header "Accept", "application/json" to the HTTP Request body field.

Ok, now my problem is to understand what do this piece of code:



In succes case call a function having a parameter named json that simply call a method named showSuccessResponse passing to it the json obkect converted to String and the link.

Now, I know what showSuccessResponse do (I use it to create a span tag that show a message in my page next to the clicked link).

the problem is: who is the json parameter that I pass to the function in the success case. I am converting to String this parameter but: when and where I have created id?

If I execute my example when I click on the link appear me (next to the link) the following message: {"foo":"bar","fruit":"apple"}

Seems ad it was created an JSON object having the following key\value contents:


foo: bar
fruit: apple


But where is it created ?!?! boooo

When I click on the link and the method is executed I have the following message in my stacktrace:



Can you help me to undestand what happen?

Thanks
Andrea

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So JQuery created it from the response it received from the server

"towards the FOLDER: /mapping/produces and not towards a .json file"

To help clear something up here. It is not a folder or a file. It is a URL. a routing path in a URL meaning the url contains the server/domain address, then the app name, then the path. If .json is ther eit is not to point to a file that ends in .json it is still just part of the URL path. It is not directories and files.

So when JQuery has its ajax method, it takes some callback functions. And it knows how to create the parameter and pass it to that callback function.

Mark
 
Andrea Nobili
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Spritzler wrote:So JQuery created it from the response it received from the server

"towards the FOLDER: /mapping/produces and not towards a .json file"

To help clear something up here. It is not a folder or a file. It is a URL. a routing path in a URL meaning the url contains the server/domain address, then the app name, then the path. If .json is ther eit is not to point to a file that ends in .json it is still just part of the URL path. It is not directories and files.

So when JQuery has its ajax method, it takes some callback functions. And it knows how to create the parameter and pass it to that callback function.

Mark



Mmm I think that now is more clear for me but it still remains quite obscure for me...I try to tell you what I understood and what I'm missing in your reasoning (Please, have patience with a newbye)

So you say me that "/mapping/produces" is not a folder but it is an URL (this one: http://localhost:8080/spring-mvc-showcase/mapping/produces ) that will be handled in some way from my controller...ok...I think that this is clear

But what exactly happens in the execution of my function? After your explanation I got the following idea:

1) The user click on the link and the callback function is execute

2) At the beginning simply put in the link variable the referer of the clicked tag (it will be used when the output message will be displayed next to this link)

3) The AJAX call function is called

4) Before send the ajax request to the server check if my called URL end with ".json", my URL don't end with ".json" so, in this case, the body of the if is not executed

5) Now the ajax request is send to the controller of the web application to be correctly handled. This request is handled by a method of my MappingController class, this one:



This method handlet GET HTTP Request towards the URL "/mapping/produces" and I think that produces=MediaType.APPLICATION_JSON_VALUE mean that create and return an object in JSON format (and this object in JSON format will be returned in the body of my HTTP Response because I have the @ResponseBody annotation for this method)

So it is created and returned a new JavaBean object.

In my package I have a JavaBean class that just creates an object with foo and fruit fields initialized



6) So, this new JavaBean object was returned to the client in JSON format inside the body of the HTTP Response. So now we are again in the JQuery script that have received the HTTP Response from the server. So we are in the success case:

Ok...this is the reason why the function take a json parameter, this parameter contain the returned object in JSON format and now is call the method that have to display the value of the String conversion JSON object as output value next to the clicked link

Now I think that I have understand, is it all right?

Thank you very much for your courtesy and professionalism












  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was perfect.

Mark
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic