Fix the issue that fastjson serializes basic type as string with quotes #256
This commit is contained in:
65
fizz-common/src/main/java/we/util/TypeUtils.java
Normal file
65
fizz-common/src/main/java/we/util/TypeUtils.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2021 the original author or authors.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package we.util;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
/**
|
||||
* @author Francis Dong
|
||||
*/
|
||||
|
||||
public abstract class TypeUtils {
|
||||
|
||||
public static boolean isBasicType(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (obj instanceof String) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof Integer) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof Long) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof Double) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof Float) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof Boolean) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof Byte) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof Short) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof Character) {
|
||||
return true;
|
||||
}
|
||||
if (obj instanceof BigDecimal) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -59,6 +59,7 @@ import we.proxy.FizzWebClient;
|
||||
import we.proxy.http.HttpInstanceService;
|
||||
import we.util.JacksonUtils;
|
||||
import we.util.MapUtil;
|
||||
import we.util.TypeUtils;
|
||||
import we.xml.JsonToXml;
|
||||
import we.xml.XmlToJson;
|
||||
import we.xml.XmlToJson.Builder;
|
||||
@@ -363,10 +364,19 @@ public class RequestInput extends RPCInput implements IInput{
|
||||
if (CONTENT_TYPE_XML.equals(reqContentType) || CONTENT_TYPE_TEXT_XML.equals(reqContentType)) {
|
||||
// convert JSON to XML if it is XML content type
|
||||
request.put("jsonBody", request.get("body"));
|
||||
String jsonStr = JSON.toJSONString(request.get("body"));
|
||||
String jsonStr = null;
|
||||
if (TypeUtils.isBasicType(request.get("body"))) {
|
||||
jsonStr = request.get("body").toString();
|
||||
} else {
|
||||
jsonStr = JSON.toJSONString(request.get("body"));
|
||||
}
|
||||
LOGGER.info("jsonBody={}", jsonStr);
|
||||
JsonToXml jsonToXml = new JsonToXml.Builder(jsonStr).build();
|
||||
body = jsonToXml.toString();
|
||||
if (jsonStr.startsWith("{") || jsonStr.startsWith("[")) {
|
||||
JsonToXml jsonToXml = new JsonToXml.Builder(jsonStr).build();
|
||||
body = jsonToXml.toString();
|
||||
} else {
|
||||
body = jsonStr;
|
||||
}
|
||||
request.put("body", body);
|
||||
LOGGER.info("body={}", body);
|
||||
LOGGER.info("headers={}", JSON.toJSONString(headers));
|
||||
@@ -379,7 +389,11 @@ public class RequestInput extends RPCInput implements IInput{
|
||||
} else if (CONTENT_TYPE_FORM_URLENCODED.equals(reqContentType)) {
|
||||
body = BodyInserters.fromFormData(MapUtil.toMultiValueMap((Map<String, Object>) request.get("body")));
|
||||
} else {
|
||||
body = JSON.toJSONString(request.get("body"));
|
||||
if (TypeUtils.isBasicType(request.get("body"))) {
|
||||
body = request.get("body").toString();
|
||||
} else {
|
||||
body = JSON.toJSONString(request.get("body"));
|
||||
}
|
||||
}
|
||||
|
||||
HttpMethod aggrMethod = HttpMethod.valueOf(inputContext.getStepContext().getInputReqAttr("method").toString());
|
||||
|
||||
Reference in New Issue
Block a user