Announcement Announcement Module
No announcement yet.
[Spring MVC] Having some problem to understand how to manage JSON request Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Spring MVC] Having some problem to understand how to manage JSON request

    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:

            <a id="byProducesAcceptJson" class="writeJsonLink" href="<c:url value="/mapping/produces" />">By produces via Accept=application/json</a>
    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:

    $("a.writeJsonLink").click(function() {
        var link = $(this); // Variable that referer the clicked link in the DOM
        // Execute AJAX call
            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;
    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:

            success: function(json) {
                MvcUtil.showSuccessResponse(JSON.stringify(json), link);
    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:

    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.springf[email protected]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
    Can you help me to undestand what happen?


  • #2
    Ok...solved by myself :-P