David
David

Reputation: 20073

springmvc using json response

Apologies if this is a duplicate but I couldn't find anything concrete as an example.

I have the following controller in springmvc.

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     * Simply selects the home view to render by returning its name.
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! the client locale is "+ locale.toString());

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );

        return "main";
    }

}

This means I can then access ${serverTime}, my question is, is there a way I can get this response to be a JSON response, without having to hard code all the JSON conversion code in this controller. Is there a way I can just put some XML in a config so it will know to convert the response into say...

{ "serverTime" : "12 12 2012" } (ignore the face this probably isn't in the right date format)

I should mention, the "main" is the name of the view (main.jsp), so I want to keep this working the same way.

Upvotes: 1

Views: 772

Answers (2)

bvulaj
bvulaj

Reputation: 5133

Annotate your method with @ResponseBody.

Then just return your item, formattedDate.

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! the client locale is "+ locale.toString());

        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        String formattedDate = dateFormat.format(date);

        model.addAttribute("serverTime", formattedDate );

        return "main";
    }

    @RequestMapping(value = "/serverTime", method = RequestMethod.GET)
    @ResponseBody
    public String serverTime(Locale locale, Model model) {
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

        return dateFormat.format(date);
    }

Upvotes: 1

JazzHands
JazzHands

Reputation: 73

There's a library for converting Java objects to JSON called gson:

http://code.google.com/p/google-gson/

Incidentally, if you're wanting to send an Ajax response rather than refreshing the page, add @ResponseBody to your method declaration:

public @ResponseBody String home(Locale locale, Model model) { .. }

and return your JSON string (assuming you're not updating your model if this is the case).

Upvotes: 0

Related Questions