55import javax .xml .bind .JAXBContext ;
66import javax .xml .bind .JAXBException ;
77
8+ import org .codehaus .jackson .map .AnnotationIntrospector ;
9+ import org .codehaus .jackson .map .DeserializationConfig ;
10+ import org .codehaus .jackson .map .ObjectMapper ;
11+ import org .codehaus .jackson .map .SerializationConfig ;
12+ import org .codehaus .jackson .map .annotate .JsonRootName ;
13+ import org .codehaus .jackson .map .annotate .JsonSerialize .Inclusion ;
14+ import org .codehaus .jackson .map .introspect .JacksonAnnotationIntrospector ;
815import org .glassfish .jersey .client .JerseyClient ;
916import org .glassfish .jersey .client .JerseyClientFactory ;
10- import org .openstack .api .identity .JaxbContextResolver ;
17+ import org .glassfish .jersey .media .json .JsonFeature ;
18+ import org .openstack .model .common .JsonRootElement ;
1119
1220public enum RestClient {
1321
@@ -21,11 +29,15 @@ private RestClient() {
2129
2230 client = (JerseyClient ) JerseyClientFactory .newClient ();
2331
32+ client .configuration ().enable (new JsonFeature ());
33+
34+ client .configuration ().register (OpenStackObjectMapperProvider .class );
35+
2436 //client.configuration().enable(JsonFeature.getInstance());
2537
26- client .configuration ().register (GsonProvider .class );
38+ // client.configuration().register(GsonProvider.class);
2739
28- client .configuration ().register (JaxbContextResolver .class );
40+ // client.configuration().register(JaxbContextResolver.class);
2941
3042 //client.configuration().register(ObjectMapperProvider.class);
3143
@@ -67,5 +79,28 @@ public JAXBContext getContext(Class<?> type) {
6779 }
6880
6981 }
82+
83+ public static final class OpenStackObjectMapperProvider implements ContextResolver <ObjectMapper > {
84+
85+ private final ObjectMapper objectMapper ;
86+
87+ public OpenStackObjectMapperProvider () {
88+ System .out .println ("Configure JSON" );
89+ objectMapper = new ObjectMapper ();
90+ objectMapper .configure (SerializationConfig .Feature .WRAP_ROOT_VALUE , true );
91+ objectMapper .configure (DeserializationConfig .Feature .UNWRAP_ROOT_VALUE , true );
92+ AnnotationIntrospector introspector = new JacksonAnnotationIntrospector ();
93+ objectMapper .setDeserializationConfig (objectMapper .getDeserializationConfig ().withAnnotationIntrospector (introspector ));
94+ objectMapper .setSerializationConfig (objectMapper .getSerializationConfig ().withSerializationInclusion (Inclusion .NON_NULL ).withAnnotationIntrospector (introspector ));
95+ }
96+
97+ @ Override
98+ public ObjectMapper getContext (Class <?> type ) {
99+ JsonRootName jsonRootName = type .getAnnotation (JsonRootName .class );
100+ objectMapper .configure (DeserializationConfig .Feature .UNWRAP_ROOT_VALUE , jsonRootName != null );
101+ return objectMapper ;
102+ }
103+
104+ }
70105
71106}
0 commit comments