@Provider @ConstrainedTo(value=SERVER) 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:
 
  
  <context-param>
   <param-name>resteasy.jsonp.silent</param-name>
   <param-value>true</param-value>
  </context-param>
  
 | Modifier and Type | Field and Description | 
|---|---|
| static javax.ws.rs.core.MediaType | APPLICATION_JAVASCRIPT_MEDIA_TYPE"application/javascript" media type. | 
| static javax.ws.rs.core.MediaType | APPLICATION_PLUS_JSON_TYPE"application/*+json" media type. | 
| static String | DEFAULT_CALLBACK_QUERY_PARAMETERDefault name of the query parameter with the method name. | 
| protected static com.fasterxml.jackson.databind.ObjectMapper | DEFAULT_MAPPERDefault  ObjectMapperfor type resolution. | 
| static MediaTypeMap<String> | jsonpCompatibleMediaTypesIf response media type is one of this jsonp response may be created. | 
| protected com.fasterxml.jackson.databind.ObjectMapper | objectMapperThe  ObjectMapperused to create typing information. | 
| protected javax.ws.rs.ext.Providers | providersThe  Providersused to retrieve theobjectMapperfrom. | 
| static javax.ws.rs.core.MediaType | TEXT_JAVASCRIPT_MEDIA_TYPE"text/javascript" media type. | 
| static javax.ws.rs.core.MediaType | TEXT_JSON_TYPE"text/json" media type. | 
| Constructor and Description | 
|---|
| Jackson2JsonpInterceptor() | 
| Modifier and Type | Method and Description | 
|---|---|
| void | aroundWriteTo(javax.ws.rs.ext.WriterInterceptorContext context) | 
| CompletionStage<Void> | asyncAroundWriteTo(AsyncWriterInterceptorContext context)Interceptor method wrapping calls to  AsyncMessageBodyWriter.asyncWriteTo(T, java.lang.Class<?>, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.ws.rs.core.MultivaluedMap<java.lang.String, java.lang.Object>, org.jboss.resteasy.spi.AsyncOutputStream)method. | 
| String | getCallbackQueryParameter()Get the name of the query parameter which contains the JavaScript method name. | 
| protected com.fasterxml.jackson.databind.ObjectMapper | getObjectMapper(Class<?> type,
               javax.ws.rs.core.MediaType mediaType)Search for an  ObjectMapperfor the given class and mediaType | 
| boolean | isWrapInTryCatch()Check is the JSONP callback will be wrapped with try-catch block | 
| void | setCallbackQueryParameter(String callbackQueryParameter)Set callback query parameter. | 
| void | setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)Set an fix  ObjectMapper. | 
| void | setProviders(javax.ws.rs.ext.Providers providers)Setter used by RESTeasy to provide the  Providers | 
| void | setUri(javax.ws.rs.core.UriInfo uri)Setter used by RESTeasy to provide the  UriInfo. | 
| void | setWrapInTryCatch(boolean wrapInTryCatch)Enables or disables wrapping the JSONP callback try try-catch block | 
public static final javax.ws.rs.core.MediaType TEXT_JAVASCRIPT_MEDIA_TYPE
public static final javax.ws.rs.core.MediaType APPLICATION_JAVASCRIPT_MEDIA_TYPE
public static final javax.ws.rs.core.MediaType TEXT_JSON_TYPE
public static final javax.ws.rs.core.MediaType APPLICATION_PLUS_JSON_TYPE
public static final String DEFAULT_CALLBACK_QUERY_PARAMETER
public static final MediaTypeMap<String> jsonpCompatibleMediaTypes
protected static final com.fasterxml.jackson.databind.ObjectMapper DEFAULT_MAPPER
ObjectMapper for type resolution. Used if none is provided by Providers.protected com.fasterxml.jackson.databind.ObjectMapper objectMapper
ObjectMapper used to create typing information.protected javax.ws.rs.ext.Providers providers
Providers used to retrieve the objectMapper from.public void aroundWriteTo(javax.ws.rs.ext.WriterInterceptorContext context)
                   throws IOException,
                          javax.ws.rs.WebApplicationException
aroundWriteTo in interface javax.ws.rs.ext.WriterInterceptorIOExceptionjavax.ws.rs.WebApplicationExceptionpublic CompletionStage<Void> asyncAroundWriteTo(AsyncWriterInterceptorContext context)
AsyncWriterInterceptorAsyncMessageBodyWriter.asyncWriteTo(T, java.lang.Class<?>, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType, javax.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 from context.
 Implementations of this method SHOULD explicitly call
 AsyncWriterInterceptorContext.asyncProceed() to invoke the next interceptor in the chain,
 and ultimately the wrapped AsyncMessageBodyWriter.asyncWriteTo method.asyncAroundWriteTo in interface AsyncWriterInterceptorcontext - invocation context.CompletionStage indicating completionprotected com.fasterxml.jackson.databind.ObjectMapper getObjectMapper(Class<?> type, javax.ws.rs.core.MediaType mediaType)
ObjectMapper for the given class and mediaTypetype - the Class to serializemediaType - the response MediaTypeObjectMapper@Context public void setUri(javax.ws.rs.core.UriInfo uri)
UriInfo.uri - the uri to set@Context public void setProviders(javax.ws.rs.ext.Providers providers)
Providersproviders - public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
ObjectMapper. If this is not set Providers are used for lookup. If there are is none too, use a default one.objectMapper - public String getCallbackQueryParameter()
public void setCallbackQueryParameter(String callbackQueryParameter)
callbackQueryParameter - the callbackQueryParameter to setgetCallbackQueryParameter()public boolean isWrapInTryCatch()
public void setWrapInTryCatch(boolean wrapInTryCatch)
wrapInTryCatch - true if you want to wrap the result with try-catch block; false otherwiseCopyright © 2020 JBoss by Red Hat. All rights reserved.