1. 程式人生 > >SpringMVC 4.1 對 jsonp 的支援

SpringMVC 4.1 對 jsonp 的支援

如:請求 http://xxxx?&callback=exec , 那麼返回的jsonp格式為 exec({"code":0, "message":"success"}); 。 其實對於格式的重新封裝並不複雜,但是對於某個請求既要支援json返回也要支援jsop返回怎麼做,那我們就得做個判斷, if(request.getParameter("callback")  != null),  如果存在就返回jsonp, 不存在就返回json。 

在使用springmvc的場景下,如何利用springmvc來返回jsonp格式,有很多方式可以實現。 這裡介紹一種比較簡單但比較通用的處理方式。前提是你使用的springmvc是4.1版本及以上

。主要是要繼承類AbstractJsonpResponseBodyAdvice, 並加入@ControllerAdvice 這個註解,basePackages 標識要被處理的controller。

實現程式碼如下:

複製程式碼
 1 @ControllerAdvice(basePackages = "xxx.controller")
 2 public class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {
 3 
 4     private final String[] jsonpQueryParamNames;
 5 
 6     public
JsonpAdvice() { 7 super("callback", "jsonp"); 8 this.jsonpQueryParamNames = new String[]{"callback"}; 9 } 10 11 @Override 12 protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType, 13 MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
14 15 HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest(); 16       17      //如果不存在callback這個請求引數,直接返回,不需要處理為jsonp 18 if (ObjectUtils.isEmpty(servletRequest.getParameter("callback"))) { 19 return; 20 } 21      //按設定的請求引數(JsonAdvice構造方法中的this.jsonpQueryParamNames = new String[]{"callback"};),處理返回結果為jsonp格式 22 for (String name : this.jsonpQueryParamNames) { 23 String value = servletRequest.getParameter(name); 24 if (value != null) { 25 MediaType contentTypeToUse = getContentType(contentType, request, response); 26 response.getHeaders().setContentType(contentTypeToUse); 27 bodyContainer.setJsonpFunction(value); 28 return; 29 } 30 } 31 } 32 }
複製程式碼