springMVC中fastjson支持jsonp的实现

fastjson支持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.apache.commons.lang3.StringUtils;
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"><!-- com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter -->
   <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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值