简体   繁体   English

Jackson POJOPropertyBuilder在POJO中找到多个setter

[英]Jackson POJOPropertyBuilder finds multiple setters in POJO

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...). 我们正在使用Spring Web MVC 3.2.2和Jackson Databind 2.4.4(以及许多其他库中......)开发一个相当大的JSON REST API。

I'm trying to use swagger-springmvc , but I'm having troubles with some our DTOs. 我正在尝试使用swagger-springmvc ,但我遇到了一些我们的DTO问题。

No matter if I use a simple @EnableSwagger or a more complex swagger config, I always get the following exception when starting Tomcat 7: 无论我使用简单的@EnableSwagger还是更复杂的swagger配置,我都会在启动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 )

The above mentioned property "year" is just one example, tried the same without the class containing the property year. 上面提到的属性“year”只是一个例子,在没有包含属性year的类的情况下尝试相同。

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 奇怪的是,杰克逊选择了两次相同的方法/设置器: javax.xml.datatype.XMLGregorianCalendar#setYear(1 params) vs javax.xml.datatype.XMLGregorianCalendar#setYear(1 params

I spent some time with the debugger and noticed that some DTOs seem to pass the check for conflicting setters just fine. 我花了一些时间与调试器,并注意到一些DTO似乎通过检查冲突的setter就好了。

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. 我在谷歌发现的这个例外的大多数页面都谈到了重载方法/设置器,而我的DTO并不是这样 - 它们只是简单的对象,只有属性,设置器和getter。

Any help would be greatly appreciated! 任何帮助将不胜感激!

The problem is that XMLGregorianCalendar has two setYear methods: setYear(int year) and setYear(BigDecimal year) . 问题是XMLGregorianCalendar有两个setYear方法: setYear(int year)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 ). 你需要告诉swagger忽略其中一个,你可以为XMLGregorianCalendar配置一个mixin ,只使用getter(类似这样 )。 This will work if XMLGregorianCalendar is only used in the context of return values. 如果XMLGregorianCalendar仅用于返回值的上下文中,则此方法将起作用。

If that is not the case, you could set up a substitution type, using the directModelSubstitute option . 如果不是这种情况,您可以使用directModelSubstitute 选项设置替换类型。 When you configure you're SwaggerSpringMvcPlugin you might try something like one of the options below 当您配置SwaggerSpringMvcPlugin您可能会尝试类似下面的选项之一

@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
}

Or if you dont care about the type being represented in your API documentation, you could ignore the type as well . 或者,如果您不关心API文档中表示的类型,也可以忽略该类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM