Replace all fastjson with jackson in nacos-api
This commit is contained in:
parent
041416790d
commit
b443279ed0
@ -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() {
|
||||
|
@ -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<T> {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JSON.toJSONString(this);
|
||||
return "ListView{" +
|
||||
"data=" + data +
|
||||
", count=" + count +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -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<Instance> hosts = new ArrayList<Instance>();
|
||||
|
||||
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)) {
|
||||
|
@ -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;
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user