Class Jackson2JsonpInterceptor

  • All Implemented Interfaces:, AsyncWriterInterceptor

    public class Jackson2JsonpInterceptor
    extends Object
    implements AsyncWriterInterceptor

    JSONP is an alternative to normal AJAX requests. Instead of using a XMLHttpRequest a script tag is added to the DOM. The browser will call the corresponding URL and download the JavaScript. The server creates a response which looks like a method call. The parameter is the body of the request. The name of the method to call is normally passed as query parameter. The method has to be present in the current JavaScript environment.

    Jackson JSON processor can produce such an response. This interceptor checks if the media type is a JavaScript one if there is a query parameter with the method name. The default name of this query parameter is "callback". So this interceptor is compatible with jQuery.

    It is possible to wrap the generated javascript function call in a try-catch block. You can enable it either by setting the wrapInTryCatch property of the provider instance to true or by setting the resteasy.jsonp.silent context-param to true:

    $Revision: 1 $
    Holger Morch
    • Field Detail


        public static final TEXT_JAVASCRIPT_MEDIA_TYPE
        "text/javascript" media type. Default media type of script tags.

        public static final APPLICATION_JAVASCRIPT_MEDIA_TYPE
        "application/javascript" media type.

        public static final TEXT_JSON_TYPE
        "text/json" media type.

        public static final APPLICATION_PLUS_JSON_TYPE
        "application/*+json" media type.

        public static final String DEFAULT_CALLBACK_QUERY_PARAMETER
        Default name of the query parameter with the method name.
        See Also:
        Constant Field Values
      • jsonpCompatibleMediaTypes

        public static final MediaTypeMap<String> jsonpCompatibleMediaTypes
        If response media type is one of this jsonp response may be created.

        protected static final com.fasterxml.jackson.databind.ObjectMapper DEFAULT_MAPPER
        Default ObjectMapper for type resolution. Used if none is provided by Providers.
      • objectMapper

        protected com.fasterxml.jackson.databind.ObjectMapper objectMapper
        The ObjectMapper used to create typing information.
      • providers

        protected providers
        The Providers used to retrieve the objectMapper from.
    • Constructor Detail

      • Jackson2JsonpInterceptor

        public Jackson2JsonpInterceptor()
    • Method Detail

      • aroundWriteTo

        public void aroundWriteTo​( context)
                           throws IOException,
        Specified by:
        aroundWriteTo in interface
      • getObjectMapper

        protected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper​(Class<?> type,
        Search for an ObjectMapper for the given class and mediaType
        type - the Class to serialize
        mediaType - the response MediaType
        the ObjectMapper
      • setUri

        public void setUri​( uri)
        Setter used by RESTeasy to provide the UriInfo.
        uri - the uri to set
      • setProviders

        public void setProviders​( providers)
        Setter used by RESTeasy to provide the Providers
        providers -
      • setObjectMapper

        public void setObjectMapper​(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
        Set an fix ObjectMapper. If this is not set Providers are used for lookup. If there are is none too, use a default one.
        objectMapper -
      • getCallbackQueryParameter

        public String getCallbackQueryParameter()
        Get the name of the query parameter which contains the JavaScript method name. Default: callback.
        the callbackQueryParameter
      • setCallbackQueryParameter

        public void setCallbackQueryParameter​(String callbackQueryParameter)
        Set callback query parameter.
        callbackQueryParameter - the callbackQueryParameter to set
        See Also:
      • isWrapInTryCatch

        public boolean isWrapInTryCatch()
        Check is the JSONP callback will be wrapped with try-catch block
        true if try-catch block is generated; false otherwise
      • setWrapInTryCatch

        public void setWrapInTryCatch​(boolean wrapInTryCatch)
        Enables or disables wrapping the JSONP callback try try-catch block
        wrapInTryCatch - true if you want to wrap the result with try-catch block; false otherwise