[Resteasy-users] Customizing Jackson mapper

2012-08-31 Thread John Reiter
I'm using JBoss AS 7.1.1.Final, the included RESTEasy distribution
(2.3.2.Final), and Jackson 1.9.9 to handle serialization/deserialization
to/from JSON.  I can serialize/deserialize JSON using @Produces/@Consumes
along with Jackson annotations in my REST resources and this all works
fine.  The problem I'm having is I'd like to start customizing some Jackson
behavior and register custom serializers/deserializers and can't quite get
this working.  This should involve getting access to the RESTEasy Jackson
ObjectMapper and running my customization code on that mapper.

After seeing something similar in a forum post, I tried this:

@Provider
@Consumes({ MediaType.APPLICATION_JSON, text/json })
@Produces({ MediaType.APPLICATION_JSON, text/json })
public class JacksonCustomizations extends ResteasyJacksonProvider {

private static final Logger log = Logger.getLogger(
JacksonCustomizations.class );

public JacksonCustomizations()
{
super();

log.info( Beginning Jackson configuration... );

final ObjectMapper mapper = _mapperConfig.getConfiguredMapper();

final Version version = new Version( 0, 0, 1, null );
final SimpleModule module = new SimpleModule( Test Module,
version );

//Add custom serializers
module.addSerializer( LocalDate.class, new
JacksonLocalDate.Serializer() );

//Add custom deserializers
module.addDeserializer( LocalDate.class, new
JacksonLocalDate.Deserializer() );

mapper.registerModule( module );

log.info( OK: Finished configuring Jackson );
}
}

But this code doesn't seem to ever run.  I receive JSON back from my
application as normal, but I don't see any log entries and my custom
serializers/deserializers don't seem to be registered (@JsonSerialize(
using = JacksonLocalDate.Serializer.class seems to have no effect).

Is this a valid approach to solving this problem or should I be going about
it in an entirely different way?  If this is valid, does anyone have any
suggestions about what might be wrong with my implementation?  If not, has
anyone had success with a different approach?

Thanks,
John
--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/___
Resteasy-users mailing list
Resteasy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/resteasy-users


Re: [Resteasy-users] Customizing Jackson mapper

2012-08-31 Thread Bill Burke


On 8/31/2012 11:13 AM, John Reiter wrote:
 I'm using JBoss AS 7.1.1.Final, the included RESTEasy distribution
 (2.3.2.Final), and Jackson 1.9.9 to handle serialization/deserialization
 to/from JSON.  I can serialize/deserialize JSON using
 @Produces/@Consumes along with Jackson annotations in my REST resources
 and this all works fine.  The problem I'm having is I'd like to start
 customizing some Jackson behavior and register custom
 serializers/deserializers and can't quite get this working.  This should
 involve getting access to the RESTEasy Jackson ObjectMapper and running
 my customization code on that mapper.

 After seeing something similar in a forum post, I tried this:

 @Provider
 @Consumes({ MediaType.APPLICATION_JSON, text/json })
 @Produces({ MediaType.APPLICATION_JSON, text/json })
 public class JacksonCustomizations extends ResteasyJacksonProvider {

  private static final Logger log = Logger.getLogger(
 JacksonCustomizations.class );

  public JacksonCustomizations()
  {
  super();

 log.info http://log.info( Beginning Jackson configuration... );

  final ObjectMapper mapper = _mapperConfig.getConfiguredMapper();

  final Version version = new Version( 0, 0, 1, null );
  final SimpleModule module = new SimpleModule( Test Module,
 version );

  //Add custom serializers
  module.addSerializer( LocalDate.class, new
 JacksonLocalDate.Serializer() );

  //Add custom deserializers
  module.addDeserializer( LocalDate.class, new
 JacksonLocalDate.Deserializer() );

  mapper.registerModule( module );

 log.info http://log.info( OK: Finished configuring Jackson );
  }
 }

 But this code doesn't seem to ever run.  I receive JSON back from my
 application as normal, but I don't see any log entries and my custom
 serializers/deserializers don't seem to be registered (@JsonSerialize(
 using = JacksonLocalDate.Serializer.class seems to have no effect).

 Is this a valid approach to solving this problem or should I be going
 about it in an entirely different way?  If this is valid, does anyone
 have any suggestions about what might be wrong with my implementation?
 If not, has anyone had success with a different approach?


Hmm, your provider should take precedence.  How are you registering your 
provider?  All ResteasyJacksonProvider does really is extend the Jackson 
one and adds produces and consumes support for application/*+json

-- 
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com

--
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
___
Resteasy-users mailing list
Resteasy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/resteasy-users