Custom ParamConverterProvider within Spring Boot for JAX-RS

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Custom ParamConverterProvider within Spring Boot for JAX-RS

Oliver Wulff-4
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




Reply | Threaded
Open this post in threaded view
|

AW: Custom ParamConverterProvider within Spring Boot for JAX-RS

Oliver Wulff-4
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 <[hidden email]>
Gesendet: Samstag, 21. November 2020 00:02
An: [hidden email] <[hidden email]>
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