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服务). 铺垫: 文件上传是 ...
随机推荐
- 逆向---01.Nop、中文字符串搜索、保存修改后程序
基础知识:(Nop:删除跳转) gcc编译链接命令: gcc -o 生成文件名 源文件名 gcc编译成汇编代码:gcc -o 生成文件名 -S 源文件名 VS查看汇编代码:(调试模式下,Ctrl+F ...
- 使用debootstrap制作debian-jessie系统docker镜像
先看一下Docker官网提示:In general, you'll want to start with a working machine that is running the distribut ...
- linux 自定义信号
从来没试过linux自定义信号,查了下,说是系统只提供了SIGUSR1和SIGUSR2两个,就两个够吗?更要命的是如果要自定义信号如#define SIG_MYSIG ....的话要改内核才行,哥 ...
- 理解for循环
先给大家出一个小题目,看看最终我们的i的值是多少? for(var i=0;i<10;i+=2){ if(i<=5){ i++; continue; }else{ i--; break; ...
- 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)
上一节,我们已经讲解了使用全连接网络实现手写数字识别,其正确率大概能达到98%,这一节我们使用卷积神经网络来实现手写数字识别, 其准确率可以超过99%,程序主要包括以下几块内容 [1]: 导入数据,即 ...
- 惊喜的gift
情侣的饰品吧!我之前在淘宝上买了一套,挺好的,很好看呢!男的手链,女的项链,手链需要项链的钥匙才能打得开,女孩子应该都会很喜欢的吧!至少我很喜欢!而且价格也不贵,150块人民币都不到,很适合当生日礼物 ...
- C#星夜拾遗之delegate示例
概念 delegate即委托.如果你使用过C++或者js中的回调函数,你会更容易明白一些.在异步操作中常常使用回调函数,例如在Ajax中,当HttpRequest执行异步请求时,就需要有一个状态改变时 ...
- SQL——将表中的最大ID+1插入新的ID中------Insert into 表 MAX(表id) +1
表结构:group表(groupid int,groupname varchar) 表中数据:id name 分组1 分组2 分组3 分组4 ----------------------------- ...
- Oracle——环比增长率
首先,了解什么是:环比增长率? 环比增长率=(本期数-上期数)÷上期数×100% 如:2014年2月的工资为:5000,2014年1月的工资为4000,则2月份的环比增长率为: (5000-4000) ...
- yolov3实践(一)
很多博友看了我的第一篇博客yolo类检测算法解析——yolo v3,对其有了一定的认识和了解,但是并没有贴出代码和运行效果,略显苍白.因此在把篇博客理论的基础上,造就了第一篇实践文章,也就是本文.只要 ...