users@jersey.java.net

[Jersey] Re: JsonP without using JSONWithPadding?

From: John Lister <john.lister_at_kickstone.com>
Date: Thu, 24 Nov 2011 23:37:21 +0000

On 24/11/2011 21:11, Ed Anuff wrote:
> I'd be interested in looking at your JacksonJsonProvider subclass if
> you'd be willing to share it. Also, did you have to do anything
> special to get Jersey to use it instead of the standard
> JacksonJsonProvider?
>
No problem, see below for the code. In terms of jersey you just need to
make sure the com.sun.jersey.config.property.packages property is set in
web.xml and includes the package path for the provider. Alternatively
make the package a subclass of your resource package.

John

ps. the code met my needs but was by no means exhaustively tested, feel
free to modify as suits

/*
  * Copyright 2011 Kickstone Technologies Ltd
  */

package com.kickstone.pricegoblin.providers;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.jaxrs.Annotations;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

/**
  *
  * @author john
  */
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Provider
public class PGJsonProducer extends JacksonJsonProvider {

     @Context HttpServletRequest servletRequest;

     public final static Annotations[] DEFAULT_ANNOTATIONS = {
        Annotations.JACKSON
     };

     public PGJsonProducer(){
         this(null, Annotations.JACKSON);
     }

     public PGJsonProducer(Annotations... annotationsToUse){
         this(null, annotationsToUse);
     }

     public PGJsonProducer(ObjectMapper mapper, Annotations[]
annotationsToUse) {
         super(mapper, annotationsToUse);
         if (mapper==null)
             mapper=_mapperConfig.getDefaultMapper();
         
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,
false);
     }

     @Override
     public long getSize(Object t, Class<?> type, Type type1,
Annotation[] antns, MediaType mt){
         long sz=super.getSize(t, type, type1, antns, mt);
         String callback=servletRequest.getParameter("callback");
         if (sz>=0 && callback!=null)
             sz+=callback.length()+2;
         return sz;
     }

     @Override
     public void writeTo(Object t, Class<?> type, Type type1,
Annotation[] antns, MediaType mt, MultivaluedMap<String, Object> mm,
OutputStream out) throws IOException, WebApplicationException{
         String callback=servletRequest.getParameter("callback");

         if (callback!=null) {
             out.write(callback.getBytes());
             out.write('(');
         }

         super.writeTo(t, type, type1, antns, mt, mm, out);

         if (callback!=null)
             out.write(')');
     }

}


-- 
www.pricegoblin.co.uk