Java 使用json响应的springmvc

Java 使用json响应的springmvc,java,json,spring-mvc,Java,Json,Spring Mvc,抱歉,如果这是一个重复,但我找不到任何具体的例子 我在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.springfram

抱歉,如果这是一个重复,但我找不到任何具体的例子

我在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";
    }

}
这意味着我可以访问${serverTime},我的问题是,有没有一种方法可以让这个响应成为JSON响应,而不必在这个控制器中硬编码所有JSON转换代码。有没有一种方法可以将一些XML放在配置中,这样它就可以知道如何将响应转换为例如

{“serverTime”:“12 12 2012”}(忽略面这可能不是正确的日期格式)


我应该提到的是,“main”是视图的名称(main.jsp),因此我希望保持这种工作方式。

有一个用于将Java对象转换为JSON的库,名为gson:

顺便说一句,如果您希望发送Ajax响应而不是刷新页面,请在方法声明中添加@ResponseBody:

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

并返回JSON字符串(如果是这种情况,则假设您没有更新模型)

有一个用于将Java对象转换为JSON的库,称为gson:

顺便说一句,如果您希望发送Ajax响应而不是刷新页面,请在方法声明中添加@ResponseBody:

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

并返回JSON字符串(如果是这种情况,则假设您没有更新模型)

@ResponseBody
注释您的方法

然后返回您的物品,
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);
    }

@ResponseBody
注释您的方法

然后返回您的物品,
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);
    }

啊,我这么做了,但是你会注意到我问题的底线,这打破了我的观点被回复的方式。两者都可以吗?啊,很抱歉。你能使用AJAX吗?加载视图main,然后请求JSON数据。是的,我的AJAX请求基本上会向控制器请求它应该访问的页面,它会这样做,但它也应该返回JSON的有效负载。你是不是建议我使用一个url作为JSON响应,另一个url作为html响应?这似乎有点令人讨厌。从根本上加载您的视图。然后视图应该在/serverTime对数据发出ajax请求。更新了一个例子。啊,我明白了,所以我必须使用两个端点,你能想到我可以改进我的设计吗?你认为让控制器处理视图是个坏主意吗?啊,我这样做了,但你会注意到我问题的底线,这打破了我返回视图的方式。两者都可以吗?啊,很抱歉。你能使用AJAX吗?加载视图main,然后请求JSON数据。是的,我的AJAX请求基本上会向控制器请求它应该访问的页面,它会这样做,但它也应该返回JSON的有效负载。你是不是建议我使用一个url作为JSON响应,另一个url作为html响应?这似乎有点令人讨厌。从根本上加载您的视图。然后视图应该在/serverTime对数据发出ajax请求。更新了一个例子。啊,我明白了,所以我必须使用两个端点,你能想到我可以改进我的设计吗?你认为让控制器处理视图是个坏主意吗?啊,如果你注意到我问题的底部,这看起来会破坏我的视图处理,因为它返回视图“main”,有可能做这个JSON响应并在控制器内继续处理我的视图吗?我想BrandonV已经回答了这个问题。啊,如果您注意到我问题的底部,这看起来会破坏我的视图处理,因为它返回视图“main”,是否可以执行此JSON响应并在控制器中继续处理我的视图?我想BrandonV已经回答了这个问题。