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.io.IOException;
31 import com.fasterxml.jackson.databind.ObjectWriter;
33 import okhttp3.MediaType;
34 import okhttp3.RequestBody;
35 import retrofit2.Converter;
37 final class JacksonRequestBodyConverter<T> implements Converter<T, RequestBody> {
38 private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
40 private final ObjectWriter adapter;
42 JacksonRequestBodyConverter(ObjectWriter adapter) {
43 this.adapter = adapter;
47 public RequestBody convert(T value) throws IOException {
48 byte[] bytes = adapter.writeValueAsBytes(value);
49 return RequestBody.create(MEDIA_TYPE, bytes);