Programming is hard by Stephan Schmidt

Scala, Maven and Jersey

As a small side note, I’ve got

import javax.ws.rs.{Path, GET, ProduceMime}

@Path("/hello")
class HelloResource {
  @GET
  @ProduceMime(Array("text/html"))
  def hello() =  "Hello World"
}

working with Maven2, Jetty and Jersey. Took some time but was interesting. Any ideas for Array("text/html") (Scala does’t support varags the same as Java does) Perhaps a clever implicit? Need to think more.

If you liked this post, subscribe to my free full RSS feed.
Filed under: Jersey, Maven, Scala

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

Get free updates by email

If you did like this article you can get free updates with your RSS reader, you can follow me on Twitter or get free update to new posts by email. Enter your email:

 
About the author: Stephan has been working as a head of development and CTO. He has experiences in different technologies since 20 years including Java, Rails and Python. Stephans main field of interest is maintainablity and productivity in software development. Want to know more? All views are only his own.

Comments

It should work as expected under 2.7.2, but until then you’re unfortunately stuck with Array(…).

stephan

Ah, very nice, thanks.

K

Array(”text/html”) === Array.apply(”text/html”) which is defined object Array (see http://www.scala-lang.org/docu/files/api/scala/Arrayobject.html#apply(A*)).

Although Scala does not support varargs, but it has a similar concept called “repeated parameters”

stephan

@K: Perhaps you can help me with converting A* to Array[A]?

(which I need for a test project, not the case of the post)

K

By default if you specified a function

def a(b:A*)

You will get b as an instance of type Array[A]

For testing purpose, you can check this in scala console.

stephan

@K: Sure? Because I though I’ve got a Seq and people on the net suggest

def g(x: Any*) =
x.asInstanceOf[scala.runtime.BoxedObjectArray].unbox(x.getClass).asInstanceOf[Array[Object]]

(which by the way works)

K

Sorry, my mistake.

Actually you get an instance of Seq (BoxedArray) of that repeated parameter but you can operate it (in Scala) like operating an Array.

If you want to operate it within the function as an Array (or pass it to Java), you must unboxed it using that suggestion.

If you want to pass it as an Array[Any] to another scala function what you only need to do is call x.toArray[Any]. Although you get another BoxedAnyArray, but when passing the parameter to a scala function, compiler will auto-unboxed it. Check following two function:

def a(is:Any*){
println(is.getClass)
println(is.toArray[Any].getClass)
b(is.toArray[Any])
}

def b(is:Array[Any]){
println(is.getClass)
println(is)
}

a(1, “String”)

you will get following output: (I tested this using Scala 2.7.1)
class scala.runtime.BoxedObjectArray
class scala.runtime.BoxedAnyArray
class [Ljava.lang.Object;
[Ljava.lang.Object;@1710808

Implementation of array in Scala is quite interesting, for detail see http://www.drmaciver.com/2008/06/scala-arrays/

Leave a Reply