Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
Spring/Spring Boot conversion - how to make a chain on Converters?

Please look at the example below:

class Foo
class Bar
class Baz

@Component
class FooToBarConverter : Converter<Foo, Bar> {
    override fun convert(source: Foo) = Bar()
}

@Component
class BarToBazConverter : Converter<Bar, Baz> {
    override fun convert(source: Bar) = Baz()
}

@RestController("/test")
class TestController(val conversionService: ConversionService) {
    @GetMapping
    fun get() = "test: " + conversionService.convert(Foo(), Baz::class.java)
    // must convert Foo to Bar, then Bar to Baz, but throws an exception instead
}

Is there any way to achieve the desired behavior without reinventing the wheel?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You Can use
@Autowired private List<Converter> allConverter;

This will inject all beans that are implementing the Converter interface.

Now you can loop throw the list and class convert method.

allConverter.stream().forEach(Converter::convert);

To Manage the order of converters, you need to sort list.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!