سؤال

I have a Jersey resource that returns a JAXB mapped object, Jersey kindly serializes this object to XML, JSON or JSONP for me with the use of the Moxy plugin when requested with the Accept header.

@GET
@JSONP( queryParam = "callback" )
@Produces( { "application/javascript", "application/json", "application/xml" } )
public Settings get()
{
   if( someCondition )
        throw new NotFoundExcpetion();
    Settings settings = new Settings();
    settings.setSomeKey( "some value" );
    return settings;
}

If the method produces an exception I have an ExceptionHandler like the one below which will produce a response containing the details of the error.

@Provider
public class NotFoundExceptionMapper implements
      ExceptionMapper<NotFoundException>
{

    @Override
    public Response toResponse( NotFoundException ex )
    {
        return Response.status( 404 )
              .entity( ex.getMessage() )
              .entity( new ErrorStatus( 404 ) )
              .build();
    }
}

However when the client requests a resource that produces an Exception and requests JSONP with an Accept header of application/javascript I get the following exception.

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/javascript, type=class com.myproject.api.dom.ErrorStatus, genericType=class com.myproject.api.dom.ErrorStatus.
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)
    at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:571)
    at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:378)
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:418)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:265)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:320)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at com.myproject.api.servlet.TransactionFilter.doFilter(TransactionFilter.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

So the question is, how can I get an ExceptionHandler to produce a JSONP response when requested?

هل كانت مفيدة؟

المحلول 2

I have a solution derived from what was suggested by @lefloh, I have simply extended the MOXyJsonProvider and added my own padding in the writeTo method. The drawback being its not compatible with Jersey's @JSONP annotation however so the annotation on the resource is now redundant. Any better solutions are welcome.

@Provider
@Produces( { "application/javascript", "application/x-javascript" } )
public class JsonMessageBodyWriter extends MOXyJsonProvider
{

    private static final MediaType APPLICATION_JAVASCRIPT = new MediaType( "application", "javascript" );
    private static final MediaType APPLICATION_XJAVASCRIPT = new MediaType( "application", "x-javascript" );

    @Context
    private HttpServletRequest httpRequest;


    @Override
    protected boolean supportsMediaType( MediaType mediaType )
    {
        if( mediaType.equals( APPLICATION_JAVASCRIPT ) ||
              mediaType.equals( APPLICATION_XJAVASCRIPT ) )
            return true;
        return false;
    }


    @Override
    public void writeTo( Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
          MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream ) throws IOException, WebApplicationException
    {

        String callback = httpRequest.getParameter( "callback" );
        if( callback == null )
            callback = "callback";

        entityStream.write( callback.getBytes() );
        entityStream.write( "(".getBytes() );
        super.writeTo( object, type, genericType, annotations, mediaType, httpHeaders, entityStream );
        entityStream.write( ")".getBytes() );
    }

}

نصائح أخرى

Problem is the entity of type com.myproject.api.dom.ErrorStatus. If this is a @XmlRootElement annotated class Jersey knows how to transform it to application/json or application/xml, but not to application/javascript.

You’ll need to provide a MessageBodyWriter for this Entity:

@Provider
@Produces("application/javascript")
public class ErrorStatusMessageBodyWriter implements MessageBodyWriter<ErrorStatus> {
  ...
}

I have not tested if annotating this provider with @JSONP works. Otherwise you’ll have to pad manually.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top