Jackson POJOPropertyBuilder在POJO中找到多个setter [英] Jackson POJOPropertyBuilder finds multiple setters in POJO
问题描述
我们正在使用Spring Web MVC 3.2.2和Jackson Databind 2.4.4(以及许多其他库中......)开发一个相当大的JSON REST API。
We're working on a rather large JSON REST API using Spring Web MVC 3.2.2 and Jackson Databind 2.4.4 (among many other libs...).
我正在尝试使用 swagger-springmvc ,但我遇到了一些麻烦DTO。
I'm trying to use swagger-springmvc, but I'm having troubles with some our DTOs.
无论我使用简单的 @EnableSwagger
还是更复杂的swagger配置,我总是得到启动Tomcat 7时出现以下异常:
No matter if I use a simple @EnableSwagger
or a more complex swagger config, I always get the following exception when starting Tomcat 7:
java.lang.IllegalArgumentException: Conflicting setter definitions for property "year": javax.xml.datatype.XMLGregorianCalendar#setYear(1 params) vs javax.xml.datatype.XMLGregorianCalendar#setYear(1 params)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getSetter(POJOPropertyBuilder.java:303)
at com.mangofactory.swagger.models.Annotations.findPropertyAnnotation(Annotations.java:33)
at com.mangofactory.swagger.models.property.bean.BeanModelProperty.<init>(BeanModelProperty.java:26)
at com.mangofactory.swagger.models.property.bean.BeanModelPropertyProvider.beanModelProperty(BeanModelPropertyProvider.java:166) (...rest of stacktrace )
上面提到的属性year只是一个例子,在没有包含属性年份的类的情况下尝试相同。
The above mentioned property "year" is just one example, tried the same without the class containing the property year.
奇怪的是,杰克逊选择了两次完全相同的方法/设置器:
javax.xml.datatype.XMLGregorianCalendar #setYear(1 params )
vs javax.xml.datatype.XMLGregorianCalendar#setYear(1 params
The weird thing is that Jackson seams to find the same exact methods / setter twice:
javax.xml.datatype.XMLGregorianCalendar#setYear(1 params)
vs javax.xml.datatype.XMLGregorianCalendar#setYear(1 params
I花了一些时间在调试器上,并注意到一些DTO似乎通过检查冲突的setter就好了。
I spent some time with the debugger and noticed that some DTOs seem to pass the check for conflicting setters just fine.
我现在花了很多时间在这上面并且不能来一个解决方案。我在谷歌发现的这个例外的大多数页面都谈到了重载方法/设置器,这与我的DTO不同 - 它们只是简单的对象,只有属性,设置器和getter。
I have now spent many hours on this and couldn't come up with a solution. Most pages for this exception I found with Google talk about overloaded methods / setters, which is not the case with my DTOs - they're simple objects with nothing more than properties, setters and getters.
非常感谢任何帮助!
推荐答案
问题是 XMLGregorianCalendar
有两个 setYear
方法: setYear(int year)
和 setYear( BigDecimal年)
。你需要告诉swagger忽略其中一个,你可以为 mixin > XMLGregorianCalendar
仅使用getter(类似于此)。如果XMLGregorianCalendar仅用于返回值的上下文中,这将起作用。
The problem is that XMLGregorianCalendar
has two setYear
methods: setYear(int year)
and setYear(BigDecimal year)
. You need to tell swagger to ignore one of them, you can configure a mixin for the XMLGregorianCalendar
to only use getters (something like this). This will work if XMLGregorianCalendar is only used in the context of return values.
如果不是这种情况,您可以使用<$ c设置替换类型$ c> directModelSubstitute 选项。当您配置 SwaggerSpringMvcPlugin
时,您可能会尝试下面的选项之一
If that is not the case, you could set up a substitution type, using the directModelSubstitute
option. When you configure you're SwaggerSpringMvcPlugin
you might try something like one of the options below
@Bean
public SwaggerSpringMvcPlugin yourPlugin() {
...
plugin.directModelSubstitute(XMLGregorianCalendar.class, String.class)
//OR this, depending on how you intend to use it and how you want
// the serialized/deserialized types to appear on the swagger UI
plugin.directModelSubstitute(XMLGregorianCalendar.class, Date.class)
...
return plugin
}
或者如果你不关心API文档中表示的类型,你可以也忽略类型。
Or if you dont care about the type being represented in your API documentation, you could ignore the type as well.
这篇关于Jackson POJOPropertyBuilder在POJO中找到多个setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!