作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个如下所示的类:
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 框架的新手,我的问题可能很愚蠢:
我知道过滤器参数应该是 jsonObject
其中包括三个 JsonArray
s。 jsonObject
中的主要参数名称应该是什么?其中包括JsonArray
s。正如你所看到的,我已经使用了类名。那是对的吗?如果我从stackoverflow
上的其他一些问题中理解正确的话,这就是当我使用基于 Spring
编写的类时应该使用的内容.
如果不正确,我怎样才能找到应该叫什么?我可以看到jsonArray
的名字只需查看类即可,因为它们是类的变量。但是 jsonObject
的名字呢?其中包括所有这些数组?
从类中,在我看来,该类还必须接受空字符串作为过滤器。但我也尝试过,但它不起作用。我应该从哪里寻找问题?
我还应该提到,我的工作是在一家公司,它是一个非常大的项目的一小部分,该项目最近已转换为 Spring 框架,我们正在测试一切是否再次正常工作。
最佳答案
根元素不需要通过 json 中的名称来引用。测试字符串应该是
{"first":[],"second":[],"third":[]}
关于java - 如何将 json 字符串作为 RESTFUL 请求发送给 postman ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45728258/
我是一名优秀的程序员,十分优秀!