diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java index b3092cd03..3d2c120e1 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/Instance.java @@ -19,9 +19,6 @@ import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.naming.PreservedMetadataKeys; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.lang3.StringUtils; @@ -179,13 +176,18 @@ public class Instance { @Override public String toString() { - ObjectMapper objectMapper = new ObjectMapper(); - objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); - try { - return objectMapper.writeValueAsString(this); - } catch (JsonProcessingException e) { - throw new RuntimeException("Instance toJson failed", e); - } + return "Instance{" + + "instanceId='" + instanceId + '\'' + + ", ip='" + ip + '\'' + + ", port=" + port + + ", weight=" + weight + + ", healthy=" + healthy + + ", enabled=" + enabled + + ", ephemeral=" + ephemeral + + ", clusterName='" + clusterName + '\'' + + ", serviceName='" + serviceName + '\'' + + ", metadata=" + metadata + + '}'; } public String toInetAddr() { diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ListView.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ListView.java index 7df2672d3..a59a531e4 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ListView.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ListView.java @@ -15,8 +15,6 @@ */ package com.alibaba.nacos.api.naming.pojo; -import com.alibaba.fastjson.JSON; - import java.util.List; /** @@ -47,6 +45,9 @@ public class ListView { @Override public String toString() { - return JSON.toJSONString(this); + return "ListView{" + + "data=" + data + + ", count=" + count + + '}'; } } diff --git a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java index 98ea5cca4..0c13c52f2 100644 --- a/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java +++ b/api/src/main/java/com/alibaba/nacos/api/naming/pojo/ServiceInfo.java @@ -15,8 +15,10 @@ */ package com.alibaba.nacos.api.naming.pojo; -import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.nacos.api.common.Constants; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -29,10 +31,12 @@ import java.util.List; * * @author nkorange */ +@JsonInclude(Include.NON_NULL) public class ServiceInfo { - @JSONField(serialize = false) + @JsonIgnore private String jsonFromServer = EMPTY; + public static final String SPLITER = "@@"; private String name; @@ -43,7 +47,6 @@ public class ServiceInfo { private long cacheMillis = 1000L; - @JSONField(name = "hosts") private List hosts = new ArrayList(); private long lastRefTime = 0L; @@ -162,7 +165,7 @@ public class ServiceInfo { return true; } - @JSONField(serialize = false) + @JsonIgnore public String getJsonFromServer() { return jsonFromServer; } @@ -171,12 +174,12 @@ public class ServiceInfo { this.jsonFromServer = jsonFromServer; } - @JSONField(serialize = false) + @JsonIgnore public String getKey() { return getKey(name, clusters); } - @JSONField(serialize = false) + @JsonIgnore public String getKeyEncoded() { try { return getKey(URLEncoder.encode(name, "UTF-8"), clusters); @@ -185,7 +188,6 @@ public class ServiceInfo { } } - @JSONField(serialize = false) public static ServiceInfo fromKey(String key) { ServiceInfo serviceInfo = new ServiceInfo(); int maxSegCount = 3; @@ -201,7 +203,7 @@ public class ServiceInfo { return serviceInfo; } - @JSONField(serialize = false) + @JsonIgnore public static String getKey(String name, String clusters) { if (!isEmpty(clusters)) { diff --git a/api/src/test/java/com/alibaba/nacos/api/annotation/NacosPropertiesTest.java b/api/src/test/java/com/alibaba/nacos/api/annotation/NacosPropertiesTest.java index a76c2585e..bdebc15f4 100644 --- a/api/src/test/java/com/alibaba/nacos/api/annotation/NacosPropertiesTest.java +++ b/api/src/test/java/com/alibaba/nacos/api/annotation/NacosPropertiesTest.java @@ -15,7 +15,6 @@ */ package com.alibaba.nacos.api.annotation; -import com.alibaba.fastjson.JSON; import org.junit.Assert; import org.junit.Test; import org.springframework.mock.env.MockEnvironment; diff --git a/api/src/test/java/com/alibaba/nacos/api/naming/pojo/ServiceInfoTest.java b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/ServiceInfoTest.java new file mode 100644 index 000000000..8c5d725e3 --- /dev/null +++ b/api/src/test/java/com/alibaba/nacos/api/naming/pojo/ServiceInfoTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.alibaba.nacos.api.naming.pojo; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Before; +import org.junit.Test; +import java.io.IOException; + +import static org.junit.Assert.*; + +public class ServiceInfoTest { + + private ObjectMapper mapper; + + private ServiceInfo serviceInfo; + + @Before + public void setUp() throws Exception { + mapper = new ObjectMapper(); + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + serviceInfo = new ServiceInfo("testName", "testClusters"); + } + + @Test + public void testSerialize() throws JsonProcessingException { + String actual = mapper.writeValueAsString(serviceInfo); + assertTrue(actual.contains("\"name\":\"testName\"")); + assertTrue(actual.contains("\"clusters\":\"testClusters\"")); + assertTrue(actual.contains("\"cacheMillis\":1000")); + assertTrue(actual.contains("\"hosts\":[]")); + assertTrue(actual.contains("\"lastRefTime\":0")); + assertTrue(actual.contains("\"checksum\":\"\"")); + assertTrue(actual.contains("\"valid\":true")); + assertTrue(actual.contains("\"allIPs\":false")); + assertFalse(actual.contains("jsonFromServer")); + assertFalse(actual.contains("key")); + assertFalse(actual.contains("keyEncoded")); + } + + @Test + public void testDeserialize() throws IOException { + String example = "{\"name\":\"testName\",\"clusters\":\"testClusters\",\"cacheMillis\":1000,\"hosts\":[],\"lastRefTime\":0,\"checksum\":\"\",\"allIPs\":false,\"valid\":true,\"groupName\":\"\"}"; + ServiceInfo actual = mapper.readValue(example, ServiceInfo.class); + assertEquals("testName", actual.getName()); + assertEquals("testClusters", actual.getClusters()); + assertEquals("", actual.getChecksum()); + assertEquals("", actual.getGroupName()); + assertEquals(1000, actual.getCacheMillis()); + assertEquals(0, actual.getLastRefTime()); + assertTrue(actual.getHosts().isEmpty()); + assertTrue(actual.isValid()); + assertFalse(actual.isAllIPs()); + } +} diff --git a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java index 34810c635..a108c1306 100644 --- a/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/utils/JacksonUtils.java @@ -57,7 +57,7 @@ public final class JacksonUtils { return mapper.readValue(json, mapper.constructType(type)); } - public static void registerSubtype(Class clz, String type) { + public static void registerSubtype(Class clz, String type) { mapper.registerSubtypes(new NamedType(clz, type)); } }