下面的部分位于Spring-mvc.xml或者dispatcherServlet-servlet.xml中 (Spring 3.0中ServletName-servlet.xml替代了Spring-mvc.xml)

<!-- 用于将对象转换为 JSON  -->
<bean id="stringConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>

在对应的Controller中

@RequestMapping(value="/login",method=RequestMethod.POST)
public @ResponseBody User login(String username,String password){
User user = userService.login(username, password);
return user;
}

这里我使用的jackson包:

      (1)jackson-core 2.5.0

      (2)jackson-databind 2.5.0

      (3)jackson-annotations 2.5.0

导入后build path;

警告:若用hibernate等orm工具生成的pojo类,一对一,对多等关系可能会输出无限循环的json:

需要使用在pojo类中导入com.fasterxml.jackson.annotation.JsonIgnore,并为需要屏蔽的类添加@JsonIgnore注解,这样被注解的属性就不会出现在json中了。

第二种示例

@ResponseBody
@RequestMapping(value = "/login")
public ModelAndView ajaxLogin(Model model,User user,HttpServletRequest request, HttpSession session){
String errorMessage=loginCommon(model, user, request, session);
Map map=new HashMap();
if(ValueWidget.isNullOrEmpty(errorMessage)){
map.put(Constant2.AJAX_LOGIN_RESULT, "success");
}else{
map.put(Constant2.AJAX_LOGIN_RESULT, "failed");
}
map.put("error", errorMessage);
model.addAttribute("user", null);
return new ModelAndView(new MappingJacksonJsonView(),map);
}

或者

model.addAttribute("user", user1);

运行结果:

Spring 之 注解实现返回json的更多相关文章

  1. Spring MVC 3.0 返回JSON数据的方法

    Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...

  2. spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500:no convertter for return value of type

    问题描述 spring Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson spring boot 新增配置解决后台返回 json 到前台中文乱码之后,出现返回json ...

  3. idea+springmvc+spring+mybatis+maven整合返回json数据webapi

    首先看一张目录结构图: : 创建步骤: 1.创建maven  webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring ...

  4. Spring WebMVC 4.1返回json时 406(Not Acceptable)

    1.问题现象Tomcat7+Spring4.1.4,返回json字符串时发生406错误 The resource identified by this request is only capable ...

  5. Spring MVC Rest服务 返回json报406错误的解决办法

    @ResponseBody & @RequestBody @RequestBody 将 HTTP 请求正文插入方法中,使用适合的HttpMessageConverter将请求体写入某个对象. ...

  6. Spring Boot 处理异常返回json

    spring boot 老版本处理异常  对于浏览器客户端,返回error数据 对于非浏览器返回json数据, 主要取决于你的请求head 是什么 但是当我们自定义了:  老版本无论请求什么都会返回j ...

  7. springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。

    springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...

  8. Spring MVC学习笔记——返回JSON对象

    1.想要GET请求返回JSON对象,首先需要导入jackson-all-1.9.4.jar包 2.在控制器中添加不同的show()方法 //show()方法返回JSON对象 @RequestMappi ...

  9. Spring MVC使用@ResponseBody返回JSON数据406以及乱码问题解决方案

    刚到一家公司,做原始的工作---接口,程序就我一人,没环境,没架构,更没旧项目可以利用,一切从0开始,经理以为我是老鸟,来就布置任务,要一周拿出结果.其实有现成架构的话写个接口还不是分分钟钟的事,关键 ...

随机推荐

  1. easyui之datagrid(不定时补充)

    1,datagrid之formatter formatter格式化函数有三个参数: value:字段值(一般为后台传递给前台的值): row:当前行数据: index:当前行索引. return值是显 ...

  2. Android开发性能优化大总结(二)

    接上篇: 1.在JAVA+Oracle 的应用系统开发中,Java中内嵌的sql语句尽量使用大写的形式,以减少Oracle解析器的解析负担: 2.由于JVM的有其自身的GC机制,但是也有遗漏的隐患,建 ...

  3. Lintcode--001(比较字符串)

    比较两个字符串A和B,确定A中是否包含B中所有的字符.字符串A和B中的字符都是 大写字母 注意事项 在 A 中出现的 B 字符串里的字符不需要连续或者有序. 您在真实的面试中是否遇到过这个题? Yes ...

  4. 破解官方recovery的签名验证

    步骤简述1.解包recovery.img,2.反编译/sbin/recovery,用ida64plus3.在反编译出来的文本中查找:signature 4.简单的看一下指令流程,CBZ下面是faile ...

  5. QuickTime 专业版 pro 注册码

    打开QuickTime Player下拉编辑菜单--选偏好设置--注册 Name: Dawn M Fredette Key: 4UJ2-5NLF-HFFA-9JW3-X2KV 重新启动 QuickTi ...

  6. cf B. Jeff and Periods

    http://codeforces.com/contest/352/problem/B #include <cstdio> #include <cstring> #includ ...

  7. SSH本地端口转发

    也是在公司常用的命令,还没有将EXPECT和SPAWN结合好,先用着: 带证书验证远程登陆的. (从公司内网服务器直接跳到外网服务器的内网端口) ssh -C -f -N -g -i private_ ...

  8. MongoDB再实测

    不用安装,直接解压.. 这些都不是最主要的,,,倒是TOMCAT和NGINX还需要更深入的了解... http://jingyan.baidu.com/article/acf728fd3d398bf8 ...

  9. 拉电流(source current)与灌电流(sink current)

    对一个互补输出的驱动器而言,从输出端向外电路流出的负载电流称为拉电流(SOURCE CURRENT):从外电路流入输出端的负载电流称为灌电流(SINK CURRENT):在没有负载的情况下,驱动器本身 ...

  10. Qt 的信号与槽机制介绍(10个要注意的问题)

    QT 是一个跨平台的 C++ GUI 应用构架,它提供了丰富的窗口部件集,具有面向对象.易于扩展.真正的组件编程等特点,更为引人注目的是目前 Linux 上最为流行的 KDE 桌面环境就是建立在 QT ...