SpringMVC中fastjson支持jsonp的实现
前边一篇文章主要说了下前端处理jsonp的方式,这篇主要介绍了后台接收和响应jsonp的一种方式
继承fastjson消息转换器类:com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter
package com.caiya.hongbao.web; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotWritableException; import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset; /**
* fastjson消息转换器
* Created by caiya on 15/12/23.
*/
public class MJFastJsonHttpMessageConverter extends FastJsonHttpMessageConverter {
public static final Charset UTF8 = Charset.forName("UTF-8");
private Charset charset;
private SerializerFeature[] features; public MJFastJsonHttpMessageConverter() {
super();
this.charset = UTF8;
this.features = new SerializerFeature[0];
} @Override
protected void writeInternal(Object obj, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
// obj就是controller中注解为@ResponseBody的方法返回值对象
if(obj instanceof JSONPObject){
JSONPObject jsonpObject = (JSONPObject)obj;
OutputStream out = outputMessage.getBody();
String text = JSON.toJSONString(jsonpObject.getJson(), this.features);
String jsonpText = new StringBuilder(jsonpObject.getFunction()).append("(").append(text).append(")").toString();
byte[] bytes = jsonpText.getBytes(this.charset);
out.write(bytes);
}else{
super.writeInternal(obj, outputMessage);
}
}
}
JSONPObject类:
package com.caiya.hongbao.web; import java.io.Serializable; /**
* Created by caiya on 15/12/23.
*/
public class JSONPObject implements Serializable { private static final long serialVersionUID = -7634081032767024781L; private String function; private Object json; public JSONPObject(String function, Object json){
this.function = function;
this.json = json;
} public String getFunction() {
return function;
} public Object getJson() {
return json;
} public JSONPObject setFunction(String function) {
this.function = function;
return this;
} public JSONPObject setJson(Object json) {
this.json = json;
return this;
} }
spring-web.xml配置:
<bean id="fastJsonHttpMessageConverter"
class="com.caiya.hongbao.web.MJFastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<ref bean="fastJsonHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
controller实例:
/**
* 个人中心红包列表、红包匹配列表
* @param status
* @param channel
* @param shouldPay
* @param orderField
* @param orderType
* @param page
* @return
*/
@RequestMapping(value = "/user/hongbao/list", method = RequestMethod.GET)
@ResponseBody
public Object hongbaoList(Integer status, String channel, Long shouldPay, String orderField, String orderType, Page page, String callback) throws SessionException {
......
UserHongbaos userHongbaos = ......
// 如果callback不为空,那么返回jsonp格式的数据
if(StringUtils.isNotBlank(callback)){
return new JSONPObject(callback, userHongbaos);
}else {
return userHongbaos;
}
}
摘自:https://my.oschina.net/wnjustdoit/blog/612146
SpringMVC中fastjson支持jsonp的实现的更多相关文章
- springMVC中Restful支持
RESTFul支持 http://localhost:8090/user/doAdd.action?username=tony&age=8 http://localhost:8090/user ...
- SpringMVC中映射路径的用法之请求限制、命名空间
SpringMVC中映射路径的请求限制 什么是SpringMVC请求限制? 在SpringMVC中,支持对请求的设置.如果不满足限制条件的话,就不让请求访问执行方法,这样可以大大提高执行方法 的安全性 ...
- SpringMVC中的参数绑定
SpringMVC中的参数绑定 参数绑定的定义 所谓参数绑定,简单来说就是客户端发送请求,而请求中包含一些数据,那么这些数据怎么到达 Controller.从客户端请求key/value数据(比如ge ...
- maven+springmvc+easyui+fastjson+pagehelper
1.maven配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- 通过扩展让ASP.NET Web API支持JSONP
同源策略(Same Origin Policy)的存在导致了"源"自A的脚本只能操作"同源"页面的DOM,"跨源"操作来源于B的页面将会被拒 ...
- SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- 如何在springMVC 中对REST服务使用mockmvc 做测试
如何在springMVC 中对REST服务使用mockmvc 做测试 博客分类: java 基础 springMVCmockMVC单元测试 spring 集成测试中对mock 的集成实在是太棒了!但 ...
- springmvc中RequestMapping的解析
在研究源码的时候,我们应该从最高层来看,所以我们先看这个接口的定义: package org.springframework.web.servlet; import javax.servlet.htt ...
- springmvc学习笔记--支持文件上传和阿里云OSS API简介
前言: Web开发中图片上传的功能很常见, 本篇博客来讲述下springmvc如何实现图片上传的功能. 主要讲述依赖包引入, 配置项, 本地存储和云存储方案(阿里云的OSS服务). 铺垫: 文件上传是 ...
随机推荐
- 基本数据类型对象包装(Integer等)
基本数据类型 包装类 byte Byte short Short int Integer long Long boolean Boolean float ...
- php redis在windows下的部署
1.下载php扩展 下载地址:https://github.com/phpredis/phpredis/downloads 2.将下载php扩展redis放到php的ext目录下,然后在php.ini ...
- Arraylist集合遍历输出
题目:创建一个只能容纳String对象名为names的Arraylist集合,按顺序向集合中添加5个字符串对象.对集合进行遍历,打印出集合中每个元素的位置与内容.首先打印出集合的大小,然后删除集合中的 ...
- 在子类中调用父类的方法super
1.没有super之前,在子类里面需要父类里面的逻辑,但是我们是通过派生(自己定义了一个init,增加了一条line) class vehichle:#定义一个交通工具的类 Country=" ...
- CentOS Linux release 7.3破解密码详解
CentOS Linux release 7.3破解密码详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 公司最近接了一个项目,拿到客户现有的源代码,但是服务器用户密码并不知情, ...
- elasticsearch索引清理
只是记录两条简单的命令 查看所有的索引文件: curl -XGET http://localhost:9200/_cat/indices?v 删除索引文件以释放空间: curl -XDELETE ht ...
- linux中vi的基本操作
在vi如何查找文字 vi redis.config 在命令模式下 按 / 然后最下方会出现/ 打出你所需要查找的字.n 是代表查找下一个 如何撤销上一步的操作 1,退出编辑操作 按esc键 2,按u ...
- UVALive 4254 Processor(二分)
题目链接 题意 有n个任务,每个任务有三个参数ri,di和wi,表示必须在时刻[ri,di]之内执行,工作量为wi.处理器执行速度可以变化,当执行速度为s时,工作量为wi.处理器的速度可以变化,当执行 ...
- bzoj千题计划307:bzoj5248: [2018多省省队联测]一双木棋
https://www.lydsy.com/JudgeOnline/problem.php?id=5248 先手希望先手得分减后手得分最大,后手希望先手得分减后手得分最小 棋盘的局面一定是阶梯状,且从 ...
- 机器学习:python使用BP神经网络示例
1.简介(只是简单介绍下理论内容帮助理解下面的代码,如果自己写代码实现此理论不够) 1) BP神经网络是一种多层网络算法,其核心是反向传播误差,即: 使用梯度下降法(或其他算法),通过反向传播来不断调 ...