Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8898769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:41:41+00:00 2026-06-15T00:41:41+00:00

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

  • 0

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:

    <li>
        <a id="byProducesAcceptJson" class="writeJsonLink" href="<c:url value="/mapping/produces" />">By produces via Accept=application/json</a>
    </li>

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
    $.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;
});

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.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

Can you help me to understand what happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:41:43+00:00Added an answer on June 15, 2026 at 12:41 am

    Take a look on the server side code as well. You will notice that the /mapping/produces URL is mapped to a handler method in MappingController:

    @RequestMapping(value="/mapping/produces", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody JavaBean byProducesJson() {
        return new JavaBean();
    }
    

    Two important things here:

    • by placing the @ResponseBody annotation on the return type Spring will serialize the returned JavaBean instance directly into the HTTP response
    • produces=MediaType.APPLICATION_JSON_VALUE will tell Spring that JSON format should be used when serializing the returned object

    As you see this controller method just returns a new instance of the JavaBean class which has the following default properties:

    public class JavaBean {
    ...
        private String foo = "bar";
        private String fruit = "apple";
    ...
    }
    

    As a result you will get the mentioned {“foo”:”bar”,”fruit”:”apple”} JSON in the response.

    also from the logs you can find out which controller method was handling your request when calling a URL:

    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()]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this period I am studing the Spring MVC showcase example (downloadable from STS
In this period I am studing the Spring MVC Showcase example dowlodable from the
I am studying the Spring MVS Showcase downloadable from di STS dashboard. I am
I have being studying (newbie) .NET and I got some doubts. Reading from a
I am quite new in the Spring MVC World. Today I am studing the
I'm studing C++ and I can't understand the meaning of the boldface sentence below:
I've got my first impression of Spring WebFlow 2.1 from studying the reference app
I have started to learn ASP.NET MVC, and at this time of studying I
I am studing java now, and one of my apps is simple Swing file
I have encountered a similar problem described here (and in other places) - where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.