You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Oliver Wulff <ow...@wowit.ch> on 2020/11/20 23:02:30 UTC

Custom ParamConverterProvider within Spring Boot for JAX-RS

Hi there

I use the cxf spring boot starter for jaxrs in version 3.3.5: cxf-spring-boot-starter-jaxrs

My REST service expects a java.time.LocalDate as a query parameter. When testing the API I get the error:
"Parameter Class java.time.LocalDate has no constructor with single String parameter, static valueOf(String) or fromString(String) methods"

I've implemented a custom ParamConverterProvider and annotated as a @Component:

@Component
public class DateParameterConverterProvider implements ParamConverterProvider {

    @Override
    public <T> ParamConverter<T> getConverter(Class<T> type, Type type1, Annotation[] antns) {
        if (LocalDate.class.equals(type)) {
            @SuppressWarnings("unchecked")
            ParamConverter<T> paramConverter = (ParamConverter<T>) new DateParameterConverter();
            return paramConverter;
        }
        return null;
    }
}

Nevertheless, the InjectionUtils class gets an Optional.empty converter here:
final Optional<ParamConverter<T>> converter = getParamConverter(pClass, genericType, paramAnns, message);


What am I missing to register my custom ParamConverterProvider.

Thanks
Oli





AW: Custom ParamConverterProvider within Spring Boot for JAX-RS

Posted by Oliver Wulff <ow...@wowit.ch>.
Hi there

I can answer my question. I've created a @Configuration class like this:

@Configuration
public class CxfConfig {

    @Autowired
    private Bus bus;

    @Autowired
    private DefaultApi apiService;

    @Autowired
    private DateParameterConverterProvider dateParameterConverterProvider;

   @Bean
    public Server rsServer() {
        JAXRSServerFactoryBean endpoint = new JAXRSServerFactoryBean();
        endpoint.setBus(bus);

        endpoint.setServiceBeans(Arrays.<Object>asList(apiService));
        endpoint.setProviders(Arrays.<Object>asList(dateParameterConverterProvider));

        return endpoint.create();
    }

}

It's important to let Spring instantiate the API implementation class (DefaultApi) as well thus you can also inject beans there as well.

Thanks
Oli

________________________________
Von: Oliver Wulff <ow...@wowit.ch>
Gesendet: Samstag, 21. November 2020 00:02
An: users@cxf.apache.org <us...@cxf.apache.org>
Betreff: Custom ParamConverterProvider within Spring Boot for JAX-RS

Hi there

I use the cxf spring boot starter for jaxrs in version 3.3.5: cxf-spring-boot-starter-jaxrs

My REST service expects a java.time.LocalDate as a query parameter. When testing the API I get the error:
"Parameter Class java.time.LocalDate has no constructor with single String parameter, static valueOf(String) or fromString(String) methods"

I've implemented a custom ParamConverterProvider and annotated as a @Component:

@Component
public class DateParameterConverterProvider implements ParamConverterProvider {

    @Override
    public <T> ParamConverter<T> getConverter(Class<T> type, Type type1, Annotation[] antns) {
        if (LocalDate.class.equals(type)) {
            @SuppressWarnings("unchecked")
            ParamConverter<T> paramConverter = (ParamConverter<T>) new DateParameterConverter();
            return paramConverter;
        }
        return null;
    }
}

Nevertheless, the InjectionUtils class gets an Optional.empty converter here:
final Optional<ParamConverter<T>> converter = getParamConverter(pClass, genericType, paramAnns, message);


What am I missing to register my custom ParamConverterProvider.

Thanks
Oli