简体   繁体   English

使用JSON响应的springmvc

[英]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. 我在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. 这意味着我可以访问$ {serverTime},我的问题是,有没有一种方法可以使此响应成为JSON响应,而不必在此控制器中硬编码所有JSON转换代码。 Is there a way I can just put some XML in a config so it will know to convert the response into say... 有没有一种方法可以将一些XML放入配置中,这样它将知道将响应转换为说...

{ "serverTime" : "12 12 2012" } (ignore the face this probably isn't in the right date format) {“ serverTime”:“ 12 12 2012”}(忽略这张面孔,日期格式可能不正确)

I should mention, the "main" is the name of the view (main.jsp), so I want to keep this working the same way. 我应该提到,“ main”是视图(main.jsp)的名称,因此我想保持此工作方式不变。

Annotate your method with @ResponseBody . 使用@ResponseBody注释您的方法。

Then just return your item, formattedDate . 然后只需返回您的项目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);
    }

There's a library for converting Java objects to JSON called gson: 有一个名为gson的用于将Java对象转换为JSON的库:

http://code.google.com/p/google-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: 顺便说一句,如果要发送Ajax响应而不是刷新页面,请在方法声明中添加@ResponseBody:

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). 并返回JSON字符串(假设是这种情况,则假设您不更新模型)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM