1 /*******************************************************************************
2 * Copyright 2017 ZTE, Inc. and others.
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
13 ******************************************************************************/
15 * Copyright (C) 2015 Square, Inc.
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
18 * in compliance with the License. You may obtain a copy of the License at
20 * http://www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software distributed under the License
23 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
24 * or implied. See the License for the specific language governing permissions and limitations under
27 package org.onap.msb.sdk.httpclient.convert.jackson;
29 import java.lang.annotation.Annotation;
30 import java.lang.reflect.Type;
32 import com.fasterxml.jackson.databind.DeserializationFeature;
33 import com.fasterxml.jackson.databind.JavaType;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import com.fasterxml.jackson.databind.ObjectReader;
36 import com.fasterxml.jackson.databind.ObjectWriter;
37 import com.fasterxml.jackson.databind.SerializationFeature;
39 import okhttp3.RequestBody;
40 import okhttp3.ResponseBody;
41 import retrofit2.Converter;
42 import retrofit2.Retrofit;
45 * A {@linkplain Converter.Factory converter} which uses Jackson.
47 * Because Jackson is so flexible in the types it supports, this converter assumes that it can
48 * handle all types. If you are mixing JSON serialization with something else (such as protocol
49 * buffers), you must {@linkplain Retrofit.Builder#addConverterFactory(Converter.Factory) add this
50 * instance} last to allow the other converters a chance to see their types.
52 public final class JacksonConverterFactory extends Converter.Factory {
53 /** Create an instance using a default {@link ObjectMapper} instance for conversion. */
54 public static JacksonConverterFactory create() {
55 ObjectMapper mapper = new ObjectMapper();
56 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
57 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
58 mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
59 return create(mapper);
62 /** Create an instance using {@code mapper} for conversion. */
63 public static JacksonConverterFactory create(ObjectMapper mapper) {
64 return new JacksonConverterFactory(mapper);
67 private final ObjectMapper mapper;
69 private JacksonConverterFactory(ObjectMapper mapper) {
71 throw new NullPointerException("mapper == null");
76 public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
78 JavaType javaType = mapper.getTypeFactory().constructType(type);
79 ObjectReader reader = mapper.reader(javaType);
80 return new JacksonResponseBodyConverter<>(reader);
84 public Converter<?, RequestBody> requestBodyConverter(Type type,
85 Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
86 JavaType javaType = mapper.getTypeFactory().constructType(type);
87 ObjectWriter writer = mapper.writerWithType(javaType);
88 return new JacksonRequestBodyConverter<>(writer);