我如何从我的弹簧控制器发送json响应? [英] How I can send json response from my spring controller?

查看:109
本文介绍了我如何从我的弹簧控制器发送json响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个主题的新手。如何从我的控制器返回JSON数据,使用类似的东西(使用ResponseBody)

I'm a newbie in the theme. How can I return JSON data from my controller, using something like that (using ResponseBody)

@RequestMapping(value = "/ajaxtest", method = RequestMethod.GET)
@ResponseBody
public Set<String> ajaxTest() {
Set<String> records = new HashSet<String>();
records.add("Record #1");
records.add("Record #2");
return records; 
}

我尝试过使用Jackson,但是出现了http 406错误。
我应该在Spring版本4.0.3中使用Jackson的正确版本以及使用的算法是什么?
UPD
Ajax电话

I tried ro use Jackson, but have http 406 error. What correct version of Jackson should I use with Spring version 4.0.3 and what is the algoritm of using? UPD Ajax call

<button id="btn">Click!</button>
<script>
$("#btn").click(
function sendAjax() {
    $.ajax({
        url: "/ajaxtest",
        dataType: "json",
        success: function(data) {
            alert(data);
        },
        error:function() {
            alert("error");
        }
    });
})
</script>


推荐答案

要转换为json请求,您必须包含以下 3 jar 到你的项目构建路径。
杰克逊jar 用于将 HTTP请求转换为jason格式。

For Converting to json request you must include following 3 jar to your project build path. Jackson jar is use for convrting HTTP request to jason format.

另请提及 headear = content-type = application / json

这些是jar文件

jackson-mapper-asl.jar
jackson-core-asl.jar
jackson-jaxrs.jar

这篇关于我如何从我的弹簧控制器发送json响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆