Custom Jackson serializer of resteasy client

后端 未结 2 390
感动是毒
感动是毒 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:47

    Alternative solution

    Looking into the source code, ResteasyJackson2Provider has the following annotations

        @Provider
        @Consumes({"application/*+json", "text/json"})
        @Produces({"application/*+json", "text/json"})
    

    so I have preferred to create a provider class with specific annotations to be sure it will be chosen first:

        public class RestEasyClientJackson {
            private ObjectMapper mapper;
    
            private JacksonJsonProvider provider;
    
            private Client client;
    
            @Before
            public void setUp() throws Exception {
               mapper = new ObjectMapper();
               mapper = mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    
               provider = new MyProvider(mapper);
    
               client = ClientBuilder.newBuilder().register(provider).build();
            }
    
            @Provider
            @Produces(MediaType.APPLICATION_JSON)
            @Consumes(MediaType.APPLICATION_JSON)
            class MyProvider extends JacksonJsonProvider {
                MyProvider(ObjectMapper mapper) {
                   super(mapper);
            }
    
            @Test
            public void myTest() throws Exception {
                // some test code...
            }
      }
    

提交回复
热议问题