Class JSONPResponseBodyAdvice

  • All Implemented Interfaces:
    org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Object>

    @Order(-2147483648)
    @ControllerAdvice
    public class JSONPResponseBodyAdvice
    extends Object
    implements org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Object>
    Created by SongLing.Dong on 7/22/2017.

    Wrap with the return object from method annotated by @ResponseJSONP in order to be serialized into jsonp format.

    url: /path/to/your/api?callback=functionName

    Since:
    Spring 4.2 when ResponseBodyAdvice is supported.

    In Spring 3.x, use method directly return a JSONPObject instead.

    See Also:
    JSONPObject, ResponseJSONP
    • Field Summary

      Fields 
      Modifier and Type Field Description
      org.apache.commons.logging.Log logger  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Object beforeBodyWrite​(Object body, org.springframework.core.MethodParameter returnType, org.springframework.http.MediaType selectedContentType, Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> selectedConverterType, org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response)  
      void beforeBodyWriteInternal​(JSONPObject jsonpObject, org.springframework.http.MediaType contentType, org.springframework.core.MethodParameter returnType, org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response)  
      protected org.springframework.http.MediaType getContentType​(org.springframework.http.MediaType contentType, org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response)
      Return the content type to set the response to.
      boolean supports​(org.springframework.core.MethodParameter returnType, Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> converterType)  
    • Field Detail

      • logger

        public final org.apache.commons.logging.Log logger
    • Constructor Detail

      • JSONPResponseBodyAdvice

        public JSONPResponseBodyAdvice()
    • Method Detail

      • supports

        public boolean supports​(org.springframework.core.MethodParameter returnType,
                                Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> converterType)
        Specified by:
        supports in interface org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Object>
      • beforeBodyWrite

        public Object beforeBodyWrite​(Object body,
                                      org.springframework.core.MethodParameter returnType,
                                      org.springframework.http.MediaType selectedContentType,
                                      Class<? extends org.springframework.http.converter.HttpMessageConverter<?>> selectedConverterType,
                                      org.springframework.http.server.ServerHttpRequest request,
                                      org.springframework.http.server.ServerHttpResponse response)
        Specified by:
        beforeBodyWrite in interface org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Object>
      • beforeBodyWriteInternal

        public void beforeBodyWriteInternal​(JSONPObject jsonpObject,
                                            org.springframework.http.MediaType contentType,
                                            org.springframework.core.MethodParameter returnType,
                                            org.springframework.http.server.ServerHttpRequest request,
                                            org.springframework.http.server.ServerHttpResponse response)
      • getContentType

        protected org.springframework.http.MediaType getContentType​(org.springframework.http.MediaType contentType,
                                                                    org.springframework.http.server.ServerHttpRequest request,
                                                                    org.springframework.http.server.ServerHttpResponse response)
        Return the content type to set the response to. This implementation always returns "application/javascript".
        Parameters:
        contentType - the content type selected through content negotiation
        request - the current request
        response - the current response
        Returns:
        the content type to set the response to