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开始,经理以为我是老鸟,来就布置任务,要一周拿出结果.其实有现成架构的话写个接口还不是分分钟钟的事,关键 ...
随机推荐
- poj1988 简单并查集
B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:30000KB 64bit ...
- JS中setAttribute的兼容性问题(摘自leejersey)
class和className兼容方法: object.setAttribute("class","content") 在IE8.Chrome.火狐.Opera ...
- C#模拟登录的htmlHelper类
public class HTMLHelper { /// <summary> /// 获取CooKie /// /// </summary> /// /// <para ...
- Hadoop学习历程(三、第一个程序)
根据之前的操作,我们已经可以正常的启动Hadoop了,关于真正的集群我会在之后进行说明.现在我们来看一下第一个程序吧 1. 在eclipse上建立一个java项目 2. 将 /usr/hadoop/s ...
- 编译protobuf-2.5.0中的错误处理
最近在编译protobuf-2.5.0源码的时候发现的错误已经应对方法 1. 在源码目录执行 ./configure 命令的时候,发生如下错误 error: C++ preprocessor &quo ...
- SuperMap
SuperMap iClient for JavaScript 新手入门 地理信息系统(英语:Geographic Information System,缩写:GIS)是一门综合性学科,结合地理学与地 ...
- cf D. Dima and Hares
http://codeforces.com/contest/358/problem/D 题意:ai代表相邻的两个野兔都没有吃食物情况下的快乐系数,bi代表的是在相邻的两个野兔中有一个吃到食物的快乐系数 ...
- cf Perfect Pair
http://codeforces.com/contest/318/problem/C #include <cstdio> #include <cstring> #includ ...
- Unity3D中C#编写脚本
1.继承MonoBehaviour类:任何一个游戏脚本都需要去继承MonoBehaviour这个类,只是在创建javascript脚本的时候,系统会将其类名与继承关系隐藏起来. 2.声明变量:使用Ja ...
- Builder模式的几个要点
1.Builder模式主要用于“分步骤构建一个复杂的对象”.在这其中“分步骤”是一个稳定的算法,而复杂对象的各个部分则经常变化.2.变化点在哪里,封装哪里——Builder模式主要在于应对“复杂对象各 ...