2017-10-16 32 views
0

工作,我希望能够调用我的API与帖子内容杰克逊功能ACCEPT_SINGLE_VALUE_AS_ARRAY不会与注释

{ 
    "property1" : "value1", 
    "property2" : "value2" 
} 

OR

[{ 
    "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-databind和jackson-core,它们仍然指向2.6.4(而jackson-dataformat-xml指向2.6.3)。我无法更新这些版本。考虑到我正在尝试更高版本的jackson-annotations,会有什么问题吗?

+0

jackson-databind包含注释配置的实现。更新jackson-annotations artifact本身并不会对你有所帮助。这种配置可能没有实现在类型级别工作;如果我有时间,我将不得不查看代码。 –

+0

好的,即使在将“jackson-databind”更新为2.7之后,我仍然看到相同的错误。对于req#2,一切正常,但请求#1,我得到HTTP 500错误。 – Tintin

+0

你有没有试过更新到最新版本的杰克逊?那将是2.9.2。如果问题在该版本中仍然存在,那么我将不得不假定此配置尚未实现在Type级别工作。为什么不在Github上为jackson-databind进行回购问题? –

回答

0

您将需要在实际属性上使用该注释,而不是包含属性的POJO。

+0

但是如果我需要将整个对象作为集合(或作为单个值)呢?我是否创建了一个新类,并让MyClass成为该类的一个属性,然后对它进行注释? ps:我也尝试过这种方法,即将MyClass包装到另一个类中并注释该属性。但即使这样做也不能解决问题!不知何故,我认为我对这些Jackson API的版本是错误的。我认为即使2.7也不会支持它。 – Tintin

+0

我不知道我理解你的问题。如果您的意思是“根值”(位于对象图顶部的值,您试图反序列化),则此功能根本无法使用。它只适用于属性。 – StaxMan