SpringMVC使用fastjson自定義Converter支持返回jsonp格式(轉)

import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import com.cim.domain.dto.JSONPObject; import org.springframework.http.HttpOutputMessage; import org.springframework.http.converter.HttpMessageNotWritableException; import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; /** * @author 鄭明亮 * @time 2017年6月1日 上午11:51:57 * @description <p>自定義轉換器,拼接jsonp格式數據 </p> * @modifyBy * @modifyTime * @modifyDescription<p> </p> */ 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); } } } 

 

修改spring-mvc.xml

將spring-mvc.xml中原來fastjson的轉換器引用類改爲自定義的Converter類html

Controller 方法實例<!-- 避免IE執行AJAX時,返回JSON出現下載文件 --><!-- com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter --> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!--改爲本身的自定義轉換類 --> <bean id="jsonConverter" class="com.zml.common.util.MJFastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> <property name="features"> <array> <value>WriteMapNullValue</value> <value>WriteNullStringAsEmpty</value> <value>QuoteFieldNames</value> <value>DisableCircularReferenceDetect</value> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>



注意返回類型寫成Object,以及添加入參callback,根據callback是否有值來判斷返回格式爲json仍是jsonpjava

/** * @author 鄭明亮 * @time 2017年6月1日 下午6:48:40 * @description <p> 根據條件查詢mongoDB監控日誌信息</p> * @modifyBy * @modifyTime * @modifyDescription<p> </p> * @param vo 日誌查詢條件擴展類 * @param typeList 監控日誌類型,增 1 刪2 改3 查4 * @param callback jsonp回調方法名稱 * @return */ @RequestMapping("/queryMonitorLogs") @ResponseBody public Object queryMonitorLogs(MonitorLogVO vo,String collectionName,Integer [] typeList,String callback){ log.info("---入參:---"+vo); if (typeList != null && typeList.length > 0) { vo.setTypeList(Arrays.asList(typeList)); } Tidings<Page<MonitorLog>> tidings = new Tidings<>(); String msg = "查詢異常"; String status = ERROR; Page<MonitorLog> page = null; try { page = monitorLogService.findByVO(vo,collectionName); if (page.getTotalCount() == 0) { msg = "查詢成功,但未查詢到數據"; status = SUCCESS_BUT_NULL; }else { msg = "查詢成功"; status = SUCCESS; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } tidings.setMsg(msg); tidings.setStatus(status); tidings.setData(page); log.info("---出參:---"+tidings); if (callback != null) { return new JSONPObject(callback,tidings); } return tidings; }
相關文章
相關標籤/搜索