You can write:
request.get("myHeader").orNull
If you want something essentially the same as your Java string. But you do not!
request.get("myHeader")
returns Option[String]
, which means Scala is a way to encourage you to write code that will not throw a null exception pointer.
You can handle Option
various ways. For example, if you want to specify a default value:
val h: String = request.get("myHeader").getOrElse("")
Or if you want to do something with the header, if it exists:
request.foreach { h: String => doSomething(h) }
Or simply:
request foreach doSomething
See this trickster for more details.
source share