Announcement Announcement Module
Collapse
No announcement yet.
[MVC]Some question about how handle HTTP Request taht produces JSON object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [MVC]Some question about how handle HTTP Request taht produces JSON object

    Hello to everyody,

    In this period I am studying the Spring MVC showcase example dowlodable form STS dashboard.

    I have some doubt about some thype of rmapping (shown in the Request Mapping section of the showcase).

    In the home.jsp view I have this two links :

    Code:
            <li>
                <a id="byProducesAcceptJson" class="writeJsonLink" href="<c:url value="/mapping/produces" />">By produces via Accept=application/json</a>
            </li>
    
            <li>
              <a id="byProducesJsonExt" class="writeJsonLink" href="<c:url value="/mapping/produces.json" />">By produces via ".json"</a>
            </li>
    The first link generate an HTTP Request towards the URL: "/mapping/produces"
    The second link generate an HTTP Request towards the URL: "/mapping/produces.json"

    Related to the writeJsonLink class I have the following simple Jquery callback function:

    Code:
    $("a.writeJsonLink").click(function() {
    
        var link = $(this);                     // Variabile che si riferisce all'elemento del DOM che ha scatenato l'evento click (il link clickato) 
    
        // Esegue la chiamata ajax 
        $.ajax({ 
            url: this.href,                     // Indirizzo verso cui  indirizzata la richiesta           
            beforeSend: function(req) {         // Prima di inviare l'HTTP Request esegui la funzione passandogli il parametro req 
                if (!this.url.match(/\.json$/)) {                               // Se l'url termina con .json 
                    req.setRequestHeader("Accept", "application/json");         // Aggiunge alla HTTP Request l'header Accept: application/json 
                }
            },
            success: function(json) {
                MvcUtil.showSuccessResponse(JSON.stringify(json), link);
            },
            error: function(xhr) {
                MvcUtil.showErrorResponse(xhr.responseText, link);
            }});
        return false;
    });
    What do this function it is very clear for me: simply execute an AJAX call and before send the call do a check to check if the URL ends with .json

    Ok, in my controller class I have the following method which handles both previous HTTP Requests:

    Code:
    @RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody JavaBean byProducesJson() {
        System.out.println("Dentro byProducesJson()");
        return new JavaBean();
    }
    The behavior of this method is quite clear for me:

    This method handles HTTP GET Request towards the URL "/mapping/produces" and the *produces element specifies that the method will not accept requests with the header named Accept incompatible with the specified MediaType and that this method produces a new object having JSON format that is put in the body field of the HTTP Response*

    The new JavaBean object contatin only two inizialized variables and getter\setter method:

    Code:
    public class JavaBean {
    
    private String foo = "bar";
    private String fruit = "apple";
    
        // Getter & Setter Method
    Ok...so a new JavaBean object is created with this variables value and this value are stored inside a JSON object that is put inside the HTTP Response towards the Browser.

    Clicking on both link, in the browser appear me the same output, this one: {"foo":"bar","fruit":"apple"}

    Now I have some doubt about it:

    1) The first link generate an HTTP Request towards: /mapping/produces

    The second link generate an HTTP Request towards: /mapping/produces.json

    My method handles HTTP Request towards the URL having form: "/mapping/produces", why this method handles also the second type of HTTP Request that end with .json?

    2) The first HTTP Request don't end with .json extension so, when the previus Jquery code is executed I don't enter in the if body and so the following header is not setted: ("Accept", "application/json") for my HTTP Request.

    Ok, the @RequestMapping annotation of my method contain the produces=MediaType.APPLICATION_JSON_VALUE element. For what I have understand reading the documentation this method have to handle the HTTP Request only if in its header there is an Accept Header that contain "Accept", "application/json" as value.

    This HTTP Request do not contain this header, why is it handled by my method?

    3)What is the difference from the 2 type of HTTP Request? are handled in the same way? why?

    Thank you very much
    Andrea
Working...
X