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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM