Anish B.
Anish B.

Reputation: 16469

spring-boot-starter-jersey is not picking up the custom converter classes automatically

I have created a sample project with spring-boot-starter-jersey.

Sample Code :

Service class

@Service
@Path("/sample")
public class SampleService {

    @Autowired
    private ConversionService conversionService;

    @GET
    @Path("/get")
    public void get() {
        Person p = new Person();
        p.setFirstName("A");
        p.setLastName("B");
        System.out.println(conversionService.convert(p, String.class));
    }

}

Sample Converter

@Component
public class PersonToNameConverter implements Converter<Person, String> {

    @Override
    public String convert(Person source) {
        return source.getFirstName() + " " + source.getLastName();
    }

}

Now, spring-boot-starter-jersey is not providing me the GenericConversionService bean automatically and also not picking up the custom converters present.

Now, I need to create a GenericConversionService bean manually to make my job possible.

@Bean
 public ConversionService conversionService() {
     ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
     Set<Converter<?, ?>> converterSet = new HashSet<>();
     converter.add(new PersonToNameConverter());
     factory.setConverters(converterSet);
     factory.afterPropertiesSet();
     return factory.getObject();
 }

pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.check</groupId>
    <artifactId>check</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>check</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

This is fine when I have less custom converter classes. But if I have too many custom converters, then it will gradually increase the code for that bean and I don't want this behaviour to happen.

Is this a bug or was not added for some reason ? If it was not added, then what should be the possible way to solve this issue ?

Upvotes: 0

Views: 518

Answers (2)

Serhii Vasniev
Serhii Vasniev

Reputation: 793

Looks like converters initialisation performs in spring-webmvc auto configuration which is a dependency of spring-boot-starter-web and not a dependency of spring-boot-starter-jersey. So that's why, probably, your converter component wasn't add to the converters list.

I think as a partial solution for adding multiple converters to the conversion service you may consider autowiring all the converters as a collection. Like this:

@Bean
public ConversionService conversionService(@Autowired Set<Converter> converters) {
    ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
    factory.setConverters(converters);
    factory.afterPropertiesSet();
    return factory.getObject();
}

Upvotes: 2

Artem Ptushkin
Artem Ptushkin

Reputation: 1299

spring-boot-starter-jersey doesn't have any specifics regarding converters initialization

Check you dependencies configuration.

It works with:

@Bean
    Converter<Cat, String> myConverter() {
        return new Converter<Cat, String>() {
            @Override
            public String convert(Cat cat) {
                return null;
            }
        };
    }

And the only spring-boot-starter-jersey and spring-boot-starter-web the latter is mandatory.

Verified by applicationContext.getBean(ConversionService.class).canConvert(Cat.class, String.class) != null

Upvotes: 0

Related Questions