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);