title: Why is my Spring Boot converter not working when others are
body: <p>I have not been able to figure out why some of my Spring Boot converters are not working. I have made a couple of converters that are working implemented the convert method, extended the WebMvcConfigurer overrode the addFormatters method and added all the converters there. some of th converters are working but I had to include some of my repos in the converter class as data from them is used in the conversion.
  some example code</p>
  <pre><code>    //this one does work
  @Component
  class ReviewToDtoConverter: Converter&lt;Review, ReviewDto&gt; {
      override fun convert(review: Review): ReviewDto {
          return ReviewDto(review)
      }
  }
  
  //this one does not work
  @Component
  class DtoToReviewConverter: Converter&lt;ReviewDto,Review&gt; {
  
      @field:Autowired
      lateinit var userRepo: UserRepo
  
      @field:Autowired
      lateinit var productRepo: ProductRepo
  
      override fun convert(reviewDto: ReviewDto): Review? {
  
          return Review(
              reviewDto = reviewDto,
              user = try {userRepo.findByIdUser(reviewDto.userId ?: throw UserNotProvidedException(&quot;The User was not provided&quot;))}
              catch (e: EmptyResultDataAccessException)
              { throw UserNotFoundException(&quot;The user could not be found&quot;)},
              product = try {productRepo.findByUpc(reviewDto.upc)}
              catch (e: EmptyResultDataAccessException)
              {throw ProductNotFoundException(&quot;The product could not be found&quot;)}
          )
      }
  }
  </code></pre>
  <p>and where i am adding the converters</p>
  <pre><code>@Configuration
  class MvcConfig: WebMvcConfigurer {
      override fun addFormatters(registry: FormatterRegistry) {
          registry.addConverter(DtoToOrderConverter())
          registry.addConverter(OrderToDtoConverter())
          registry.addConverter(UserToDtoConverter())
          registry.addConverter(DtoToUserConverter())
          registry.addConverter(DtoToProductConverter())
          registry.addConverter(ProductToDtoConverter())
          registry.addConverter(ReviewToDtoConverter())
          registry.addConverter(DtoToReviewConverter())
          super.addFormatters(registry)
  
      }
  
  }
  
  @Component
  class MyConversionService:DefaultConversionService(), ConversionService{
  
  }
  </code></pre>
  <p>I had to add the MyConversionService class because i was getting an error around spring not know which bean to use and this was the only solution i found that worked</p>
  <p>and where i am using the conversion service</p>
  <pre><code>    @RestController
  @RequestMapping(&quot;/review&quot;)
  class ReviewController(val reviewService: ReviewService, val conversionService: MyConversionService) {
  
      @PutMapping(params = [&quot;upc&quot;, &quot;userId&quot;])
      fun updateReview(@RequestParam(&quot;upc&quot;) upc: Int, @RequestParam(&quot;userId&quot;) userId: Int, @RequestBody reviewDto: ReviewDto): ReviewDto {
          reviewDto.userId = userId
          reviewDto.upc = upc
          conversionService.addConverter(DtoToReviewConverter())
          println(&quot;can convert &quot; + conversionService.canConvert(ReviewDto::class.java, Review::class.java))
  
          return conversionService.convert(reviewService.updateReview(upc, userId, conversionService.convert(reviewDto, Review::class.java) ?: throw CustomConversionException(&quot;There was a problem&quot;)), ReviewDto::class.java) ?: throw CustomConversionException(&quot;There was a problem&quot;)
  
      }
  
      @PostMapping(params = [&quot;upc&quot;, &quot;userId&quot;])
      fun saveReview(@RequestParam(&quot;upc&quot;) upc: Int, @RequestParam(&quot;userId&quot;) userId: Int, @RequestBody reviewDto: ReviewDto): ReviewDto {
          reviewDto.userId = userId
          reviewDto.upc = upc
          return conversionService.convert(reviewService.saveReview(
              upc = upc,
              userId = userId,
              review = conversionService.convert(reviewDto, Review::class.java) ?: throw CustomConversionException(&quot;There was a problem&quot;)
          ), ReviewDto::class.java) ?: throw CustomConversionException(&quot;There was a problem&quot;)
      }
  
      @GetMapping(params = [&quot;userId&quot;])
      fun getReviewsByUser(@RequestParam(&quot;userId&quot;) userId: Int): List&lt;ReviewDto&gt; {
          return reviewService.getReviewsByUser(userId).map { conversionService.convert(it, ReviewDto::class.java) ?: throw CustomConversionException(&quot;There was a problem&quot;) }
      }
  
      @GetMapping(params = [&quot;upc&quot;])
      fun getReviewsByProduct(@RequestParam(&quot;upc&quot;) upc: Int): List&lt;ReviewDto&gt; {
          return reviewService.getReviewsByProduct(upc).map { conversionService.convert(it, ReviewDto::class.java) ?: throw CustomConversionException(&quot;There was a problem&quot;) }
      }
  }
  </code></pre>
  <p>here is the stack trace</p>
  <pre><code>org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [com.example.demo.datatranferobjects.ReviewDto] to type [com.example.demo.entities.Review]
      at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:322) ~[spring-core-5.3.6.jar:5.3.6]
      at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195) ~[spring-core-5.3.6.jar:5.3.6]
      at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175) ~[spring-core-5.3.6.jar:5.3.6]
      at com.example.demo.controllers.ReviewController.updateReview(ReviewController.kt:18) ~[classes/:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.6.jar:5.3.6]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.6.jar:5.3.6]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.3.6.jar:5.3.6]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) ~[spring-webmvc-5.3.6.jar:5.3.6]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.6.jar:5.3.6]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.6.jar:5.3.6]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060) ~[spring-webmvc-5.3.6.jar:5.3.6]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962) ~[spring-webmvc-5.3.6.jar:5.3.6]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.6.jar:5.3.6]
      at org.springframework.web.servlet.FrameworkServlet.doPut(FrameworkServlet.java:920) ~[spring-webmvc-5.3.6.jar:5.3.6]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.45.jar:4.0.FR]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.6.jar:5.3.6]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.45.jar:4.0.FR]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.45.jar:9.0.45]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.6.jar:5.3.6]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.6.jar:5.3.6]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.6.jar:5.3.6]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.6.jar:5.3.6]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:93) ~[spring-boot-actuator-2.4.5.jar:2.4.5]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.6.jar:5.3.6]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.6.jar:5.3.6]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.3.6.jar:5.3.6]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]
      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.45.jar:9.0.45]
      at java.base/java.lang.Thread.run(Thread.java:831) ~[na:na]
  </code></pre>
  <p>i tried adding the repos to one of the working converter classes thinking it might stop working but it did still work</p>
  <p>not sure where to go as the stack trace to me isnt that helpful</p>
  <p><em><strong>Update</strong></em> I have made some progress i think and am pretty sure the issue has to do with the order spring is instantiating the beans. I am just not sure how to set everything up correctly now. Seems like if I could change how spring is doing the autowiring I could fix the issue</p>