Custom Jackson serializer of resteasy client

后端 未结 2 391
感动是毒
感动是毒 2021-01-19 19:32

Is it possible to register a custom Jackson JSON serializer for the resteasy client?

I\'ve tried to do something like:

ResteasyClient client = new Re         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-19 19:41

    You can only register JAX-RS components like Providers or Features. You need to register an Provider which carries an ObjectMapper which carries an Module with your serializer registered.

    ResteasyJacksonProvider resteasyJacksonProvider = new ResteasyJacksonProvider();
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule myModule = new SimpleModule("myModule", new Version(1, 0, 0, null));
    myModule.addSerializer(Custom.class, new CustomSerializer());
    mapper.registerModule(myModule);
    resteasyJacksonProvider.setMapper(mapper);
    ResteasyClient client = new ResteasyClientBuilder().register(resteasyJacksonProvider).build();
    

    Note that this example is using the classes from org.codehaus.jackson. The API from com.fasterxml.jackson looks slightly different.

提交回复
热议问题