Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 映射字符串x=";[……”是;到JsonNode导致额外的报价(Spring Boot)_Java_Json_Spring_Spring Boot_Jackson - Fatal编程技术网

Java 映射字符串x=";[……”是;到JsonNode导致额外的报价(Spring Boot)

Java 映射字符串x=";[……”是;到JsonNode导致额外的报价(Spring Boot),java,json,spring,spring-boot,jackson,Java,Json,Spring,Spring Boot,Jackson,为了编写某个特性的测试,我必须将包含[]的字符串强制转换到JsonNode中 问题是,当将它映射到JsonNode时,它似乎在向它添加额外的引号 我期望的是“[]”,但我得到的是“[]”,这导致测试失败。当我在正常操作中调试代码时,在使用Postman测试代码时,我只得到“[]”,而不是在测试期间得到的不工作的“[]” 这就是我的DTO在Spring Boot中的样子 import com.fasterxml.jackson.annotation.JsonCreator; import com.

为了编写某个特性的测试,我必须将包含
[]
的字符串强制转换到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作为
labels
JsonNode对象放入
PostLabelDTO
的一个新实例中进行测试,但它不起作用

这是通过postman处理的请求(与中一样,它抛出正确的异常)

因此,基本上我尝试将上述Json作为
labels
JsonNode对象放入
PostLabelDTO
的一个新实例中进行测试,但它不起作用

这是一个工作请求(返回一个带有标签的PDF,每个页面上都有一个标签)

注意 标签的模式(在该请求中称为每个标签的数据)始终可以根据要填充的模板而变化。因此,无法创建包含所有属性的Label对象,因为这些属性总是不同的(取决于要填充此请求的模板的HTML)。我的服务根据标记属性名称执行“搜索和替换”

我已经试过了:

但是,我似乎无法像预期的那样向JsonNode对象添加空数组

有人能帮我吗

问候,


Ali

因此,代码中的基本内容是:
最终字符串jsonString=“[]”;

但是,在邮递员的请求中,您将
标签作为type
Array
发送,而不是type
String

由于这里没有太多信息,您可以向本机构查询:

{
"templateId":"5b1140608134691d1804e74e",
"labels":"[]"
}
并共享输出,甚至这(类型的差异)可能会帮助您理解在尝试将数组转换为字符串类型时遇到的意外行为。在测试类中,您应该尝试从arrayType获取JsonNode 测试类中的字符串[]标签


更新: 当使用
String[]
而不是
String
作为标签时,您还需要在从
String[]
获取
JsonNode
时给出一个传递类型。
.

因此,代码中基本上是这样的:
最终字符串jsonString=“[]”;

但是,在邮递员的请求中,您将
标签作为type
Array
发送,而不是type
String

由于这里没有太多信息,您可以向本机构查询:

{
"templateId":"5b1140608134691d1804e74e",
"labels":"[]"
}
并共享输出,甚至这(类型的差异)可能会帮助您理解在尝试将数组转换为字符串类型时遇到的意外行为。在测试类中,您应该尝试从arrayType获取JsonNode 测试类中的字符串[]标签


更新: 当使用
String[]
而不是
String
作为标签时,您还需要在从
String[]
获取
JsonNode
时给出一个传递类型。
.

PostLabelDTO

public class PostLabelDTO {

    private final String templateId;

    private final String[] labels;

    public String[] getLabels() {
        return labels;
    }

    @JsonCreator
    public PostLabelDTO(final @JsonProperty("templateId") String templateId,
            final @JsonProperty("labels") String[] labels) {
        this.templateId = templateId;
        this.labels = labels;
    }

    public String getId() {
        return templateId;
    }

}
然后按如下方式测试此代码:

String str[] = {};
PostLabelDTO postLabelDTO = new PostLabelDTO("fj2j931j2ijd1", str);
对于使用那个大JSON的实际请求,您应该具有如下所示的
PostLabelDTO

public class PostLabelDTO {

@JsonProperty("templateId")
private String templateId;
@JsonProperty("labels")
private List<Label> labels = null;
....
}
公共类PostLabelDTO{
@JsonProperty(“模板ID”)
私有字符串模板ID;
@JsonProperty(“标签”)
私有列表标签=null;
....
}

尝试使用link从JSON生成POJO,它将生成正确的JAVA类,您可以使用实际的JSON请求正文测试使用此调用的工作。

尝试使用
标签作为
String[]
PostLabelDTO

public class PostLabelDTO {

    private final String templateId;

    private final String[] labels;

    public String[] getLabels() {
        return labels;
    }

    @JsonCreator
    public PostLabelDTO(final @JsonProperty("templateId") String templateId,
            final @JsonProperty("labels") String[] labels) {
        this.templateId = templateId;
        this.labels = labels;
    }

    public String getId() {
        return templateId;
    }

}
然后按如下方式测试此代码:

String str[] = {};
PostLabelDTO postLabelDTO = new PostLabelDTO("fj2j931j2ijd1", str);
对于使用那个大JSON的实际请求,您应该具有如下所示的
PostLabelDTO

public class PostLabelDTO {

@JsonProperty("templateId")
private String templateId;
@JsonProperty("labels")
private List<Label> labels = null;
....
}
公共类PostLabelDTO{
@JsonProperty(“模板ID”)
私有字符串模板ID;
@JsonProperty(“标签”)
私有列表标签=null;
....
}

尝试使用link从JSON生成POJO,它将生成正确的JAVA类,您可以使用实际的JSON请求体测试使用此调用的工作。

这给了我
{“timestamp”:“2018-06-01T16:50:53.377+0000”,“status”:500,“error”:“Internal Server error”,“message”:JSONArray文本必须以“[”开头,位于1[字符2第1行],“路径”:“/labels/”}
我已经尝试在到达
PostLabelDTO
时将其转换为
JSONArray
而不是
JsonNode
,但它似乎无法解析它,并且我得到一个错误,即它的语法不正确,因此构造函数总是希望有一个JsonNode?,当您从postman发送请求时,它会映射到PostLabelDTO,但没有从您的测试类中获取?对吗?是的,它不经过测试类,但是通常的路径(@JsonProperty等)您可能需要在获取数组时传递类型,或者从这里尝试一些东西:基本上,您只需要从字符串数组中获取JsonNode这给了我
{“timestamp”:“2018-06-01T16:50:53.377+0000”,“status”:500,“错误”:“内部服务器错误”,“消息”:“JSONArray文本必须以“[”开头,位于1[字符2第1行]”,“路径”:“/labels/”}
我已经尝试在到达
PostLabelDTO
时将其转换为
JSONArray
而不是
JsonNode
,但是它似乎无法解析它,并且我得到一个错误,即它的语法不正确,因此构造函数总是期望一个JsonNode?,当您从postman发送请求时,它会被映射到