Spring 之 注解实现返回json
下面的部分位于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的更多相关文章
- Spring MVC 3.0 返回JSON数据的方法
Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...
- spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500:no convertter for return value of type
问题描述 spring Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson spring boot 新增配置解决后台返回 json 到前台中文乱码之后,出现返回json ...
- idea+springmvc+spring+mybatis+maven整合返回json数据webapi
首先看一张目录结构图: : 创建步骤: 1.创建maven webapp工程, 创建完后的目录结构为: 2.添加项目依赖(添加jar包) 需要的jar包: spring-webmvc, spring ...
- Spring WebMVC 4.1返回json时 406(Not Acceptable)
1.问题现象Tomcat7+Spring4.1.4,返回json字符串时发生406错误 The resource identified by this request is only capable ...
- Spring MVC Rest服务 返回json报406错误的解决办法
@ResponseBody & @RequestBody @RequestBody 将 HTTP 请求正文插入方法中,使用适合的HttpMessageConverter将请求体写入某个对象. ...
- Spring Boot 处理异常返回json
spring boot 老版本处理异常 对于浏览器客户端,返回error数据 对于非浏览器返回json数据, 主要取决于你的请求head 是什么 但是当我们自定义了: 老版本无论请求什么都会返回j ...
- springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。
springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...
- Spring MVC学习笔记——返回JSON对象
1.想要GET请求返回JSON对象,首先需要导入jackson-all-1.9.4.jar包 2.在控制器中添加不同的show()方法 //show()方法返回JSON对象 @RequestMappi ...
- Spring MVC使用@ResponseBody返回JSON数据406以及乱码问题解决方案
刚到一家公司,做原始的工作---接口,程序就我一人,没环境,没架构,更没旧项目可以利用,一切从0开始,经理以为我是老鸟,来就布置任务,要一周拿出结果.其实有现成架构的话写个接口还不是分分钟钟的事,关键 ...
随机推荐
- 今日分享一点干货。PHP中课程表的实现。
首先贴代码,代码贴完再细说: 前段HTML: <div id="studentRead" class="reading" style="z-in ...
- js动态给table添加行(tr)
html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> < ...
- PYTHON-进阶-ITERTOOLS模块
PYTHON-进阶-ITERTOOLS模块小结 这货很强大, 必须掌握 文档 链接 pymotw 链接 基本是基于文档的翻译和补充,相当于翻译了 itertools用于高效循环的迭代函数集合 组成 总 ...
- cf D. Queue
http://codeforces.com/contest/353/problem/D #include <cstdio> #include <cstring> #includ ...
- DPI情况下处理
1. 字体不要跟着变大小,那就要使用setPixe,不要使用setPointSize 2. 图片可设置QPixmap::setDevicePixelRatio http://doc.qt.io/qt- ...
- 哪些产品不用开发原生APP,微信公众号就够了?
最近一阶段H5技术被推到高峰,很多人认为借助H5就能利用微信公众号取代APP原生应用了,而事实是怎么样的?这里我从产品层做一个客观分析. 一,原生APP总体趋势 要谈APP是否会被微信取代,那么必须回 ...
- Spring中Bean的命名问题及ref和idref之间的区别
一直在用Spring,其实对其了解甚少,刚去了解了一下Spring中Bean的命名问题以及ref和idref之间的区别,略作记录,以备后查. Spring中Bean的命名 1.每个Bean可以有一个i ...
- Resty 一款极简的restful轻量级的web框架
https://github.com/Dreampie/Resty Resty 一款极简的restful轻量级的web框架 开发文档 如果你还不是很了解restful,或者认为restful只是一种规 ...
- 深入浅出Node.js (8) - 构建Web应用
8.1 基础功能 8.1.1 请求方法 8.1.2 路径解析 8.1.3 查询字符串 8.1.4 Cookie 8.1.5 Session 8.1.6 缓存 8.1.7 Basic认证 8.2 数据上 ...
- Html.DropDownList的用法
直接上代码 页面代码 <td> <%= Html.DropDownList("selCity") %> </td> controller里面的代 ...