ajax获取数据的3种方式和springmvc消息转换器
来源:互联网 发布:sql去掉重复字段 编辑:程序博客网 时间:2024/06/18 08:19
controller层:
1.ajax获取数据的最原始方式:
传入参数 OutputStream os
os.write(通过第三方json-lib转换的json字符串.getByte())
@RequestMapping(value = "/queryAFood", method = RequestMethod.GET)
public String queryFoodList(OutputStream outputStream, String foodName)
throws Exception {
List<Map<String, Object>> queryFoodList = ajaxFoodService
.queryFoodList(foodName);
JSONArray jsonArray = JSONArray.fromObject(queryFoodList);
String jString = jsonArray.toString();
outputStream.write(jString.getBytes("UTF-8"));
return null;
}
2. 直接返回 字节数组 + @ResponseBody 注解
减少流的输出动作代码
os.write(jsonStr.getBytes("UTF-8"));
@ResponseBody
@RequestMapping(value = "/queryAFoodByte", method = RequestMethod.GET)
public byte[] queryFoodByte(OutputStream outputStream, String foodName)
throws Exception {
List<Map<String, Object>> queryFoodList = ajaxFoodService
.queryFoodList(foodName);
JSONArray jsonArray = JSONArray.fromObject(queryFoodList);
String jString = jsonArray.toString();
return jString.getBytes("UTF-8");
}
3. 直接返回对象 springmvc自动转换成json:需要配置消息转换器,引入jar包
@ResponseBody
@RequestMapping(value = "/queryAFoodList", method = RequestMethod.GET)
public List<Map<String, Object>> queryFoodList(OutputStream outputStream, String foodName)
throws Exception {
List<Map<String, Object>> queryFoodList = ajaxFoodService
.queryFoodList(foodName);
return queryFoodList;
}
jar包依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>
配置消息转换器 xx-servlet.xml中添加:
<mvc:annotation-driven >
<mvc:message-converters>
<!-- 配置返回byte[]的消息转换器 -->
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html</value>
<value>application/x-www-form-urlencoded</value>
</list>
</property>
</bean>
<!-- 配置返回对应解析成json的消息转换器 -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html</value>
<value>application/x-www-form-urlencoded</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
1.ajax获取数据的最原始方式:
传入参数 OutputStream os
os.write(通过第三方json-lib转换的json字符串.getByte())
@RequestMapping(value = "/queryAFood", method = RequestMethod.GET)
public String queryFoodList(OutputStream outputStream, String foodName)
throws Exception {
List<Map<String, Object>> queryFoodList = ajaxFoodService
.queryFoodList(foodName);
JSONArray jsonArray = JSONArray.fromObject(queryFoodList);
String jString = jsonArray.toString();
outputStream.write(jString.getBytes("UTF-8"));
return null;
}
2. 直接返回 字节数组 + @ResponseBody 注解
减少流的输出动作代码
os.write(jsonStr.getBytes("UTF-8"));
@ResponseBody
@RequestMapping(value = "/queryAFoodByte", method = RequestMethod.GET)
public byte[] queryFoodByte(OutputStream outputStream, String foodName)
throws Exception {
List<Map<String, Object>> queryFoodList = ajaxFoodService
.queryFoodList(foodName);
JSONArray jsonArray = JSONArray.fromObject(queryFoodList);
String jString = jsonArray.toString();
return jString.getBytes("UTF-8");
}
3. 直接返回对象 springmvc自动转换成json:需要配置消息转换器,引入jar包
@ResponseBody
@RequestMapping(value = "/queryAFoodList", method = RequestMethod.GET)
public List<Map<String, Object>> queryFoodList(OutputStream outputStream, String foodName)
throws Exception {
List<Map<String, Object>> queryFoodList = ajaxFoodService
.queryFoodList(foodName);
return queryFoodList;
}
jar包依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.12</version>
</dependency>
配置消息转换器 xx-servlet.xml中添加:
<mvc:annotation-driven >
<mvc:message-converters>
<!-- 配置返回byte[]的消息转换器 -->
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html</value>
<value>application/x-www-form-urlencoded</value>
</list>
</property>
</bean>
<!-- 配置返回对应解析成json的消息转换器 -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html</value>
<value>application/x-www-form-urlencoded</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
阅读全文
0 0
- ajax获取数据的3种方式和springmvc消息转换器
- AJAX+SpringMVC 获取后台数据的方式
- Ajax获取数据的几种格式和解析方式
- Ajax获取数据的几种格式和解析方式
- ajax获取数据的两种方式
- ajax方式和springMVC数据交互
- SpringMVC(SSM)获取网页数据和传出数据的几种方式
- SpringMVC(SSM)获取网页数据和传出数据的几种方式
- SpringMVC(SSM)获取网页数据和传出数据的几种方式
- SpringMvc 消息转换器
- springMVC配置消息转换器
- 实现SpringMvc消息转换器
- SpringMvc自定义消息转换器
- SpringMvc自定义消息转换器
- struts2,springmvc RequestPayload数据获取的方式
- Ajax-jquery获取json数据的三种方式
- springmvc--数据转换器
- SpringMVC源码-消息转换器HttpMessageConverter
- Linux Ubuntu 屏幕显示不全 可选分辨率太渣
- 记录。心情(2)
- go println与printf区别
- 【操作系统】处理器的二级调度
- 76.笔记 MySQL学习——C编写MySQL程序九预处理
- ajax获取数据的3种方式和springmvc消息转换器
- java并发编程--Runnable Callable及Future
- 【动态规划】Alternating Strings Gym
- 使用Phoenix api操作hbase 报错java.lang.ClassNotFoundException: org.apache.phoenix.jdbc.PhoenixDriver
- HBase(0.96以上版本)过滤器Filter详解及实例代码
- HashMap的原理及实现
- 链接脚本分析
- 【Pta测试6.1】:基础编程题目集
- Ubuntu17上安装Tensorflow