2 * Copyright 2016-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
14 package org.onap.msb.sdclient.wrapper.util;
16 import java.util.List;
19 import org.onap.msb.sdclient.core.CatalogService;
20 import org.onap.msb.sdclient.core.HealthService;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.SerializationFeature;
29 public class JacksonJsonUtil {
31 private static final Logger logger = LoggerFactory.getLogger(JacksonJsonUtil.class);
33 private static ObjectMapper mapper;
38 * @param createNew 方式:true,新实例;false,存在的mapper实例
41 public static synchronized ObjectMapper getMapperInstance() {
43 mapper = new ObjectMapper();
55 public static String beanToJson(Object obj) throws Exception {
58 ObjectMapper objectMapper = getMapperInstance();
59 objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
60 json = objectMapper.writeValueAsString(obj);
61 } catch (Exception e) {
62 logger.error("Class beanToJson faild:" + e.getMessage());
63 throw new Exception("Class beanToJson faild:" + e.getMessage());
73 * @param json 准备转换的json字符串
78 public static Object jsonToBean(String json, Class<?> cls) throws Exception {
81 ObjectMapper objectMapper = getMapperInstance();
82 objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
83 vo = objectMapper.readValue(json, cls);
85 } catch (Exception e) {
86 logger.error(cls + " JsonTobean faild:" + e.getMessage());
87 throw new Exception(cls + " JsonTobean faild:" + e.getMessage());
97 * @param json 准备转换的json字符串
102 public static List<CatalogService> jsonToListBean(String json) {
103 List<CatalogService> vo = null;
106 ObjectMapper objectMapper = getMapperInstance();
109 vo = objectMapper.readValue(json, new TypeReference<List<CatalogService>>() {});
111 } catch (Exception e) {
112 logger.error(" JsonTobean faild:" + e.getMessage());
117 public static <T> T jsonToListBean(String json, TypeReference<T> valueTypeRef) {
120 ObjectMapper objectMapper = getMapperInstance();
122 return objectMapper.readValue(json, valueTypeRef);
124 } catch (Exception e) {
125 logger.error(" JsonTobean faild:" + e.getMessage());
133 * 将json字符串转换成java集合对象
135 * @param json 准备转换的json字符串
140 public static Map<String, String[]> jsonToMapBean(String json) {
141 Map<String, String[]> vo = null;
144 ObjectMapper objectMapper = getMapperInstance();
147 vo = objectMapper.readValue(json, new TypeReference<Map<String, String[]>>() {});
149 } catch (Exception e) {
150 logger.error("JsonTobean faild");