java - 将 String x = "[]" 映射到 JsonNode 会导致额外的引号(Spring Boot)
问题描述
为了为某个功能编写测试,我必须将包含的字符串[]
转换为 JsonNode。
问题是当它映射到 JsonNode 时,它似乎添加了额外的引号。
我期望的是“[]”,但我得到的是“[]”,这会导致测试失败。当我在正常操作中调试代码时,在使用 Postman 测试代码时我确实只得到“[]”,而不是在测试期间才得到的不工作的“[]”。
这就是我的 DTO 在 Spring Boot 中的样子
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
public class PostLabelDTO {
private final String templateId;
private final String labels;
@JsonCreator
public PostLabelDTO(
final @JsonProperty("templateId") String templateId,
final @JsonProperty("labels") JsonNode labels
) {
this.templateId = templateId;
this.labels = labels.toString();
}
public String getId() {
return templateId;
}
public String getData() {
return labels;
}
}
我的测试必须用属性伪造这个对象,以传递给将要测试的方法。
我的测试如下所示:
@Test
public void getEmptyDocumentException() throws InvalidBarcodeException, EmptyStringException, InvalidBarcodeGeometryException, EmptyFieldException, TemplateNotFoundException, InvalidBarcodeStrategyException {
//defining an ID for the templateId JsonProperty
final String templateId = "fj2j931j2ijd1";
//this is the "labels" JsonNode that gets sent in through the Post request
//i checked 10 times how the value comes into the DTO and it was always "[]" (empty labels (document) object, for which I wrote this test for)
final String jsonString = "[]";
ObjectMapper mapper = new ObjectMapper();
mapper.enable(JsonParser.Feature.ALLOW_MISSING_VALUES);
JsonNode labels = mapper.valueToTree(jsonString);
//when I do this, the "[]" which is normally passed into the PostLabelDTO, becomes ""[]"", so there are extra quotes added
PostLabelDTO dto = new PostLabelDTO(templateId, labels);
final Document document = new Document(dto, templateRepository);
Exception resultingException = null;
try {
document.getPDF();
} catch (Exception e) {
e.printStackTrace();
assertThat(resultingException).isExactlyInstanceOf(EmptyDocumentException.class);
}
}
所以基本上我试图将上面的 Json 放入一个新的实例中PostLabelDTO
作为labels
JsonNode 对象用于测试目的,但它不起作用。
这是它通过邮递员工作的请求(它的工作原理是,它抛出正确的异常)
{
"templateId":"5b1140608134691d1804e74e",
"labels":[]
}
所以基本上我试图将上面的 Json 放入一个新的实例中PostLabelDTO
作为labels
JsonNode 对象用于测试目的,但它不起作用。
这是一个工作请求(它返回一个带有标签的 PDF,每页上都有一个标签)
{
"templateId": "5b1140608134691d1804e74e",
"labels": [{
"data": {
"Ivolgnr": "Volgnr",
"Ilkw-nr": "Ilkw-nr",
"bedrijf": "Hornbach",
"wagenNr": "13513542626",
"barcode": {
"waarde": "9780471117094"
},
"leverdatumVan": "x",
"leverdatumNaar": "x",
"orderList": [{
"order": [{
"articlenumber": "29-840-4512"
},
{
"description": "Mooie grote plant"
},
{
"ordernumber": "3584479012860361"
},
{
"amount": "20"
},
{
"sellprice": "€5,00"
},
{
"deliverydate": "01-09-2018"
}
]
},
{
"order": [{
"articlenumber": "29-840-4512"
},
{
"description": "Mooie grote plant"
},
{
"ordernumber": "3584479012860361"
},
{
"amount": "20"
},
{
"sellprice": "€5,00"
},
{
"deliverydate": "01-09-2018"
}
]
},
{
"order": [{
"articlenumber": "29-840-4512"
},
{
"description": "Mooie grote plant"
},
{
"ordernumber": "3584479012860361"
},
{
"amount": "20"
},
{
"sellprice": "€5,00"
},
{
"deliverydate": "01-09-2018"
}
]
},
{
"order": [{
"articlenumber": "29-840-4512"
},
{
"description": "Mooie grote plant"
},
{
"ordernumber": "3584479012860361"
},
{
"amount": "20"
},
{
"sellprice": "€5,00"
},
{
"deliverydate": "01-09-2018"
}
]
},
{
"order": [{
"articlenumber": "29-840-4512"
},
{
"description": "Mooie grote plant"
},
{
"ordernumber": "3584479012860361"
},
{
"amount": "20"
},
{
"sellprice": "€5,00"
},
{
"deliverydate": "01-09-2018"
}
]
}
]
}
}, {
"data": {
"Ivolgnr": "22324rff",
"Ilkw-nr": "246426246",
"bedrijf": "bedrijfffff",
"wagenNr": "wagennrrrrrrr",
"barcode": {
"waarde": "9780471117094"
},
"leverdatumVan": "x",
"leverdatumNaar": "x",
"orderList": [{
"order": [{
"articlenumber": "a"
},
{
"description": "b"
},
{
"ordernumber": "c"
},
{
"amount": "d"
},
{
"sellprice": "e"
},
{
"deliverydate": "f"
}
]
}]
}
}]
}
注意 标签的架构(在此请求中称为每个标签的数据)总是会根据要填充的模板而有所不同。因此,不可能创建包含所有属性的 Label 对象,因为这些属性总是不同的(取决于要填充此请求的模板的 HTML。我的服务基于标签属性名称执行“搜索和替换”。
我已经尝试过: 如何将 JSON 字符串解析为杰克逊的 JsonNode?
但我似乎无法按预期向 JsonNode 对象添加一个空数组。
有人可以帮帮我吗?
问候,
阿里
解决方案
所以,基本上你在代码中的内容是这样的:
final String jsonString = "[]";
但是,在邮递员的请求中,您发送labels
的是 typeArray
而不是 type String
。
由于这里没有太多信息,您可以查看此正文:
{
"templateId":"5b1140608134691d1804e74e",
"labels":"[]"
}
并共享输出,甚至这个(类型差异)可能会帮助您了解在尝试将数组转换为字符串类型时所面临的意外行为。在测试类中,您应该尝试从 arrayType 获取 JsonNode
String[] labels in test class
更新:在使用String []
而不是String
for 标签时,您还需要在JsonNode
从String []
.
请参阅此链接以了解如何执行此操作。
推荐阅读
- kubernetes - Kubernetes微服务部署集群图
- python - 索引/有序队列?
- microsoft-teams - 我可以为 Polly 或 Forms 等 MicrosoftTeams 创建上下文菜单条目吗
- gnuplot - 在gnuplot中添加百分比符号
- javascript - Javascript 进行事件调度的有效方法,
- c++ - C++ 代码来测试另一个 .cpp(如果可以编译它)
- r - as.Date 返回 NA
- google-sheets - 标记 Google 表格公式开头的字符是什么?
- bash - 从文件 A 中删除文件 B 的行
- unity3d - 在 2d unity 自上而下的游戏中发射激光