the blog for developers

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.

You can leave a Reply here. Of course, you should follow me on twitter here.

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.

About the author: Stephan Schmidt is head of development at brands4friends. He has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.
Leave a reply.

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/Array$object.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

What people wrote somewhere else:

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?