Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC, handlign a JSON request...I have some proble to understand
    text
    copied!<p>I am studing the Spring MVC showwcase dowloadable from the STS dashboard.</p> <p>Now I am studing how Spring MVC mapping the resources and I have some problem to understand the following thing:</p> <p>So, I have the following link that generate an HTTP Request towards the "/mapping/produces" folder:</p> <pre><code> &lt;li&gt; &lt;a id="byProducesAcceptJson" class="writeJsonLink" href="&lt;c:url value="/mapping/produces" /&gt;"&gt;By produces via Accept=application/json&lt;/a&gt; &lt;/li&gt; </code></pre> <p>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:</p> <pre><code>$("a.writeJsonLink").click(function() { var link = $(this); // Variable that referer the clicked link in the DOM // Execute AJAX call $.ajax({ url: this.href, // Address to which the request is addressed beforeSend: function(req) { // Before send the Http Request call a function passing it the referer to the HTTP Request if (!this.url.match(/\.json$/)) { // If the url of the clicked link end with .json req.setRequestHeader("Accept", "application/json"); // Add to the HTTP Request theheader Accept: application/json } }, success: function(json) { MvcUtil.showSuccessResponse(JSON.stringify(json), link); }, error: function(xhr) { MvcUtil.showErrorResponse(xhr.responseText, link); }}); return false; }); </code></pre> <p>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.</p> <p>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.</p> <p>Ok, now my problem is to understand what do this piece of code:</p> <pre><code> success: function(json) { MvcUtil.showSuccessResponse(JSON.stringify(json), link); }, </code></pre> <p>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.</p> <p>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).</p> <p>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?</p> <p>If I execute my example when I click on the link appear me (next to the link) the following message: <strong>{"foo":"bar","fruit":"apple"}</strong></p> <p>Seems ad it was created an JSON object having the following key\value contents:</p> <p><strong>foo: bar</strong></p> <p><strong>fruit: apple</strong></p> <p>But where is it created ?!?! boooo</p> <p>When I click on the link and the method is executed I have the following message in my stacktrace:</p> <pre><code>01:27:38 [tomcat-http--28] DispatcherServlet - DispatcherServlet with name 'appServlet' processing GET request for [/spring-mvc-showcase/mapping/produces] 01:27:38 [tomcat-http--28] RequestMappingHandlerMapping - Looking up handler method for path /mapping/produces 01:27:38 [tomcat-http--28] RequestMappingHandlerMapping - Returning handler method [public org.springframework.samples.mvc.mapping.JavaBean org.springframework.samples.mvc.mapping.MappingController.byProducesJson()] 01:27:38 [tomcat-http--28] DispatcherServlet - Last-Modified value for [/spring-mvc-showcase/mapping/produces] is: -1 01:27:38 [tomcat-http--28] RequestResponseBodyMethodProcessor - Written [JavaBean {foo=[bar], fruit=[apple]}] as "application/json" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@360eb2b8] 01:27:38 [tomcat-http--28] DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'appServlet': assuming HandlerAdapter completed request handling 01:27:38 [tomcat-http--28] DispatcherServlet - Successfully completed request </code></pre> <p>Can you help me to undestand what happen?</p> <p>Thanks Andrea</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload