Class Jackson2JsonpInterceptor
- All Implemented Interfaces:
WriterInterceptor,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:
<context-param>
<param-name>resteasy.jsonp.silent</param-name>
<param-value>true</param-value>
</context-param>
- Version:
- $Revision: 1 $
- Author:
- Holger Morch
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MediaType"application/javascript" media type.static final MediaType"application/*+json" media type.static final StringDefault name of the query parameter with the method name.protected static final ObjectMapperDefaultObjectMapperfor type resolution.static final MediaTypeMap<String>If response media type is one of this jsonp response may be created.protected ObjectMapperTheObjectMapperused to create typing information.protected ProvidersTheProvidersused to retrieve theobjectMapperfrom.static final MediaType"text/javascript" media type.static final MediaType"text/json" media type. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaroundWriteTo(WriterInterceptorContext context) Interceptor method wrapping calls toAsyncMessageBodyWriter.asyncWriteTo(T, java.lang.Class<?>, java.lang.reflect.Type, java.lang.annotation.Annotation[], jakarta.ws.rs.core.MediaType, jakarta.ws.rs.core.MultivaluedMap<java.lang.String, java.lang.Object>, org.jboss.resteasy.spi.AsyncOutputStream)method.Get the name of the query parameter which contains the JavaScript method name.protected ObjectMappergetObjectMapper(Class<?> type, MediaType mediaType) Search for anObjectMapperfor the given class and mediaTypebooleanCheck is the JSONP callback will be wrapped with try-catch blockvoidsetCallbackQueryParameter(String callbackQueryParameter) Set callback query parameter.voidsetObjectMapper(ObjectMapper objectMapper) Set an fixObjectMapper.voidsetProviders(Providers providers) Setter used by RESTeasy to provide theProvidersvoidSetter used by RESTeasy to provide theUriInfo.voidsetWrapInTryCatch(boolean wrapInTryCatch) Enables or disables wrapping the JSONP callback try try-catch block
-
Field Details
-
TEXT_JAVASCRIPT_MEDIA_TYPE
"text/javascript" media type. Default media type of script tags. -
APPLICATION_JAVASCRIPT_MEDIA_TYPE
"application/javascript" media type. -
TEXT_JSON_TYPE
"text/json" media type. -
APPLICATION_PLUS_JSON_TYPE
"application/*+json" media type. -
DEFAULT_CALLBACK_QUERY_PARAMETER
Default name of the query parameter with the method name.- See Also:
-
jsonpCompatibleMediaTypes
If response media type is one of this jsonp response may be created. -
DEFAULT_MAPPER
DefaultObjectMapperfor type resolution. Used if none is provided byProviders. -
objectMapper
TheObjectMapperused to create typing information. -
providers
TheProvidersused to retrieve theobjectMapperfrom.
-
-
Constructor Details
-
Jackson2JsonpInterceptor
public Jackson2JsonpInterceptor()
-
-
Method Details
-
aroundWriteTo
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException Interceptor method wrapping calls toMessageBodyWriter.writeTo(T, java.lang.Class<?>, java.lang.reflect.Type, java.lang.annotation.Annotation[], jakarta.ws.rs.core.MediaType, jakarta.ws.rs.core.MultivaluedMap<java.lang.String, java.lang.Object>, java.io.OutputStream)method. The parameters of the wrapped method called are available fromcontext. Implementations of this method SHOULD explicitly callWriterInterceptorContext.proceed()to invoke the next interceptor in the chain, and ultimately the wrappedMessageBodyWriter.writeTomethod.- Specified by:
aroundWriteToin interfaceWriterInterceptor- Parameters:
context- invocation context.- Throws:
IOException- if an IO error arises or is thrown by the wrappedMessageBodyWriter.writeTomethod.WebApplicationException- thrown by the wrappedMessageBodyWriter.writeTomethod.
-
asyncAroundWriteTo
Description copied from interface:AsyncWriterInterceptorInterceptor method wrapping calls toAsyncMessageBodyWriter.asyncWriteTo(T, java.lang.Class<?>, java.lang.reflect.Type, java.lang.annotation.Annotation[], jakarta.ws.rs.core.MediaType, jakarta.ws.rs.core.MultivaluedMap<java.lang.String, java.lang.Object>, org.jboss.resteasy.spi.AsyncOutputStream)method. The parameters of the wrapped method called are available fromcontext. Implementations of this method SHOULD explicitly callAsyncWriterInterceptorContext.asyncProceed()to invoke the next interceptor in the chain, and ultimately the wrappedAsyncMessageBodyWriter.asyncWriteTomethod.- Specified by:
asyncAroundWriteToin interfaceAsyncWriterInterceptor- Parameters:
context- invocation context.- Returns:
- a
CompletionStageindicating completion
-
getObjectMapper
Search for anObjectMapperfor the given class and mediaType- Parameters:
type- theClassto serializemediaType- the responseMediaType- Returns:
- the
ObjectMapper
-
setUri
Setter used by RESTeasy to provide theUriInfo.- Parameters:
uri- the uri to set
-
setProviders
Setter used by RESTeasy to provide theProviders- Parameters:
providers-
-
setObjectMapper
Set an fixObjectMapper. If this is not setProvidersare used for lookup. If there are is none too, use a default one.- Parameters:
objectMapper-
-
getCallbackQueryParameter
Get the name of the query parameter which contains the JavaScript method name. Default: callback.- Returns:
- the callbackQueryParameter
-
setCallbackQueryParameter
Set callback query parameter.- Parameters:
callbackQueryParameter- the callbackQueryParameter to set- See Also:
-
isWrapInTryCatch
public boolean isWrapInTryCatch()Check is the JSONP callback will be wrapped with try-catch block- Returns:
- 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- Parameters:
wrapInTryCatch- true if you want to wrap the result with try-catch block; false otherwise
-