2013年3月13日 星期三

Jersey Client with Customize JSON (Jackson) Configuration


Below code allow user assign its own Jackson ObjectMapper with Jersey Client instead of the default one.


    
    ClientConfig clientConfig = new DefaultClientConfig();
    // Allow Jersey Client support JSON.
    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);   
    // Create your own Jackson ObjectMapper.
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // Create your own JacksonJaxbJsonProvider and then assign it to the config.
    JacksonJaxbJsonProvider jacksonProvider = new JacksonJaxbJsonProvider();
    jacksonProvider.setMapper(mapper);
    clientConfig.getSingletons().add(jacksonProvider); 

    Client client = Client.create(clientConfig); 

1 則留言:

  1. Here is an updated version for JAX-RS 2.0

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    JacksonJaxbJsonProvider jacksonProvider = new JacksonJaxbJsonProvider();
    jacksonProvider.setMapper(objectMapper);
    Client restClient = ClientBuilder.newClient(new ClientConfig(jacksonProvider));

    回覆刪除