gpt4 book ai didi

java - 如何将 json 字符串作为 RESTFUL 请求发送给 postman

转载 作者:行者123 更新时间:2023-12-02 12:21:42 27 4
gpt4 key购买 nike

我有一个如下所示的类:

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.util.StringUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

@Data
public class MyTestClass {
@JsonProperty("first")
private List<String> first = new ArrayList<>();
@JsonProperty("second")
private List<String> second = new ArrayList<>();
@JsonProperty("third")
private List<String> third = new ArrayList<>();

public static MyTestClass emptyFilter() {
return new MyTestClass();
}


@SuppressWarnings("unused") //JSR311
@Nonnull
public static MyTestClass fromString(@Nullable String jsonString) {
if (StringUtils.isEmpty(jsonString)){
return emptyFilter();
}
return JsonUtils.fromJSONString(jsonString, MyTestClass.class, false);
}

}

我的问题是关于RESTfull 调用网络服务的输入。我的请求必须有 filter参数应该引用这个类。因此,我给出的过滤器参数如下:

{"MyTestClass":{"first":[],"second":[],"third":[]}}

但我总是收到此错误:

Failed to convert value of type 'java.lang.String' to required type 'MyTestClass'

我是 Spring 框架的新手,我的问题可能很愚蠢:

  1. 我知道过滤器参数应该是 jsonObject其中包括三个 JsonArray s。 jsonObject 中的主要参数名称应该是什么?其中包括JsonArray s。正如你所看到的,我已经使用了类名。那是对的吗?如果我从stackoverflow上的其他一些问题中理解正确的话,这就是当我使用基于 Spring 编写的类时应该使用的内容.

  2. 如果不正确,我怎样才能找到应该叫什么?我可以看到jsonArray的名字只需查看类即可,因为它们是类的变量。但是 jsonObject 的名字呢?其中包括所有这些数组?

  3. 从类中,在我看来,该类还必须接受空字符串作为过滤器。但我也尝试过,但它不起作用。我应该从哪里寻找问题?

我还应该提到,我的工作是在一家公司,它是一个非常大的项目的一小部分,该项目最近已转换为 Spring 框架,我们正在测试一切是否再次正常工作。

最佳答案

根元素不需要通过 json 中的名称来引用。测试字符串应该是

{"first":[],"second":[],"third":[]}

关于java - 如何将 json 字符串作为 RESTFUL 请求发送给 postman ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45728258/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com