我希望能够使用 POST 内容调用我的 api
{
"property1" : "value1",
"property2" : "value2"
}
或者
[{
"property1" : "value1",
"property2" : "value2"
}]
为此,我用注释了 java 类
@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
public class MyClass {
private String porperty1;
private String property2;
public MyClass() {
super();
// TODO Auto-generated constructor stub
}
public MyClass(String porperty1, String property2) {
super();
this.porperty1 = porperty1;
this.property2 = property2;
}
public String getPorperty1() {
return porperty1;
}
public void setPorperty1(String porperty1) {
this.porperty1 = porperty1;
}
public String getProperty2() {
return property2;
}
public void setProperty2(String property2) {
this.property2 = property2;
}
}
在 servlet 中我有一个像这样的 api
Response myAPI(Collection<MyClass> reqs, @Context HttpServletResponse httpResponse, @Context SecurityContext securityContext) {
}
但是我在第一个请求类型中不断收到内部服务器错误 500。
我尝试将 jackson=annotations 版本从 2.6.0 更新到 2.7.0,但它似乎在任何版本中都不起作用。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.0</version>
</dependency>
注意:我的 POM 中有其他 jackson 依赖项,例如jackson-databind 和 jackson-core,它们仍然指向 2.6.4(并且 jackson-dataformat-xml 指向 2.6.3)。我无法更新这些版本。考虑到我正在尝试使用更高版本的 jackson 注释,这会有任何问题吗?
最佳答案
您需要在实际属性上使用该注释,而不是在包含属性的 POJO 上使用。
关于java - Jackson 功能 ACCEPT_SINGLE_VALUE_AS_ARRAY 不适用于注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46780240/