java - Jackson 功能 ACCEPT_SINGLE_VALUE_AS_ARRAY 不适用于注释

标签 java rest jackson

我希望能够使用 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/

相关文章:

java - 尝试使用 Collections.sort 对我的数组进行排序,但没有成功

java - 将 META-INF/context.xml 重新加载到 ../conf/Catalina/localhost/myapp.xml 不起作用

entity-framework - 获取 RESTful 请求看到的返回计数

ios - 从 Swift 函数中的异步调用返回数据

javascript - 将 javascript 的值(value)转化为速度

java - Jackson readTree() 忽略 null 和空值

java - @JSonIgnore 注释的等效代码设置是什么?

java - 如何摆脱eclipse中过多的间距

java - 空对象引用上的 android.media.MediaPlayer.start()'

java - 如何使用查询参数控制 Jersey 序列化