Scala with Spring Boot strange behaviour: hangs and no exception on input failure

I am using the Scala. I am not so familiar with Scala, but at least it worked to use the jackson JSON parser.

Now, I have a very strange problem. If the input JSON String has a syntax error, the parse methods hangs, gives no answer and no exception. I use this object / methods, which works fine on correct input:


import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import org.json4s.jackson.JsonMethods.parse
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule

object JsonHelper {
val internal_mapper = new ObjectMapper() with ScalaObjectMapper
internal_mapper.registerModule(DefaultScalaModule)

implicit def jsonStrToAny(jsonStr: String)(implicit m : Manifest[Any]): Any = {
    internal_mapper.readValue[Any](jsonStr)
  }
}

In the Code I simply call: JsonHelper.jsonStrToAny("[{\"660\":false]")

Update:

This method is used by an kafka handler method called onReceived. @Thilo pointed out, that the exception could be "blocked" by the calling code. Could it be, that this kafka handler "swallowed" the exception?

private val checker = Consumer
    .plainSource(consumerSettings, Subscriptions.topics(CHECK_REQUEST_TOPIC))
    .toMat(Sink.foreach(onReceived))(Keep.both)
    .mapMaterializedValue(DrainingControl.apply)
    .run()

Do anyone has a hint for me, why this could be?

Thank you in advance,

Allan

1 answer

  • answered 2019-06-25 09:00 Allan Karlson

    I found myself an answer, which does not really satisfy me. If someone has an explanation you are free to comment it.

    The simple trick was to wrap a try-catch around, even there was no exeception thrown first. Now I can detect wrong input strings and handle it, however I want.

    But I don't understand why before there was no execption thrown?

    implicit def jsonStrToAny(jsonStr: String)(implicit m: Manifest[Any]): Any = {
        try {
          internal_mapper.readValue[Any](jsonStr)
        } catch {
          case e: Exception => {
            null
          }
        }
      }
    

    Reards