lundi 5 août 2019

Spring custom PropertyEditor not recognized : no matching editors or conversion strategy found

I am calling a SOAP WS using Spring's WebServiceGatewaySupport and using reflection to set the request values. Spring version is 4.2.4.RELEASE and Spring WS version is 3.0.7.RELEASE.

Some of the expected attributes are of type XmlGregorianCalendar, so I created a custom XmlGregorianCalendarPropertyEditor for String<->XmlGregorianCalendar casting :

@Slf4j
public class XmlGregorianCalendarPropertyEditor extends PropertyEditorSupport {

private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

@Override
public String getAsText() {
    XMLGregorianCalendar xmlGregorianCalendar = (XMLGregorianCalendar) getValue();
    if (xmlGregorianCalendar != null) {
        df.setTimeZone(xmlGregorianCalendar.toGregorianCalendar().getTimeZone());
        return df.format(xmlGregorianCalendar.toGregorianCalendar().getTime());
    } else {
        return "";
    }
}

@Override
public void setAsText(String text) {
    if (StringUtils.isBlank(text)) {
        setValue(null);
    } else {
        GregorianCalendar calendar = new GregorianCalendar();
        try {
            calendar.setTime(df.parse(text));
            XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
            setValue(xmlGregorianCalendar);
        } catch (ParseException e) {
            log.error("Error parsing date {}", text);
            setValue(null);
        } catch (DatatypeConfigurationException e) {
            log.error("Error building XmlGregorianCalendar!");
            setValue(null);
        }
    }
}

}

This code could probably be better, I haven't been able to test it yet.

I have tried different ways of registering my PropertyEditor within my @Configuration class, but none have worked. Here's how it is right now :

@Bean
public CustomEditorConfigurer customEditorConfigurer() {
    CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer();
    customEditorConfigurer.setCustomEditors(Collections.singletonMap(XMLGregorianCalendar.class, XmlGregorianCalendarPropertyEditor.class));
    return customEditorConfigurer;
}

Reflection-wise, I have a BeanWrapperImpl on the Request class and I simply use its method setPropertyValue(attributeName, value).

Here's the exception message I get :

Failed to convert property value of type [java.lang.String] to required type [javax.xml.datatype.XMLGregorianCalendar] for property 'dateCreationDocument'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [javax.xml.datatype.XMLGregorianCalendar] for property 'dateCreationDocument': no matching editors or conversion strategy found

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire