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 8724977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:56:11+00:00 2026-06-13T07:56:11+00:00

In my ajax response, I want to return a JSON result. I’m using spring

  • 0

In my ajax response, I want to return a JSON result.

I’m using spring mvc, and I have jackson in my pom.xml already.

Now in my controller’s action I have:

  @ResponseBody
    @RequestMapping(value = "/someAjaxResponse", method = RequestMethod.POST)
    public String someAjaxResponse(HttpServletRequest request, HttpServletResponse response) {

    }

What built in java datastructure/type do you suggest I use so I can then convert it to json using jackson?

I don’t want to create a new class for each response type, so I’m looking for a good general purpose java type for this purpose.

Suggestions?

For converting to json, which method would be best as I know jackson has multiple ways converting objects, like ObjectMapper which I believe you create a single instance of and re-use throughout the entire application? So does that mean I mark it as final?

  • 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-13T07:56:12+00:00Added an answer on June 13, 2026 at 7:56 am

    The answer depends on your use case. My suggestion is that you create a custom value object that exactly fit your needs, return it and then let Jackson handle the serializaion for you:

    @ResponseBody
    @RequestMapping(value = "/someAjaxResponse", method = RequestMethod.POST)
    public CustomValueObject someAjaxResponse(HttpServletRequest request, HttpServletResponse response) {
    
    }
    

    All that you have to do is to add the <mvc:annotation-driven /> or @EnableWebMvc to your application context, add the Jackson dependencies to your classpath and then the object will be serialized to JSON automatically because you use the @ResponseBody annotation.

    Read more about the MappingJacksonHttpMessageConverterin the Spring reference manual:

    • http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-config-enable
    • http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/remoting.html#rest-mapping-json-converter

    Note, the returned object can have complex structure. Imagine that you would like to provide person data to the client, then you can return a Person object from your controller method:

    public class Person {
        public String getFirstName() {...}
        public int getAge() {...}
        List<String> getEmailAddresses() {...}
        Address getAddress() {...}
    }
    
    public class Address {
        public String getStreet() {...}
        public int getHouseNumber() {...}
        public String getCity() {...}
        public int getZIP() {...}
        public String getState() {...}
    }
    

    Which may be serialized to:

    {
        "firstName": "John",
        "lastName": "Doe",
        "age": 42,
        "emailAddresses": [
            "john@doe.com",
            "john.doe@somewhere.com"
        ],
        "address": {
            "street": "First Avenue",
            "houseNumber": 123,
            "city": "Smallville",
            "ZIP": 12345,
            "state": "CA"
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here I have pasted my code, I want to return the response of $.ajax
In my application I have a login using ajax that returns a JSON result.
I have an ajax response with JSON. It will an anchor tags. There will
I have a response object which I want to convert to Json, but somehow
I have been using jquery ajax method to get the json data. I have
In my Ajax request (using jQuery ) I am returning a JSON response. So
I want to upload file using ajax to an ASP.Net MVC Controller My JavaScript
I am filtering an ajax response using jQuery to extract the body of a
I've got XML coming back from an Ajax.Request in Ajax.Response.responseXML . My XML actually
In the event of a successful AJAX call I want to return a simple

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.