Spray: routing - understand the difference between path and pathPrefix
path
is a final path, while pathPrefix
can be subsequently combined with other path segments using the DSL.
If you want to match exactly /hello
you should use path("hello")
.
pathPrefix
is convenient in cases like
pathPrefix("hello") {
path("foo") {
complete("foo")
} ~
path("bar") {
complete("bar")
}
}
which will match /hello/foo
and /hello/bar
.
That having being said, I suspect the error you're getting is simply the scala parser not getting along with the DSL.
Can you try moving the ~
on the same line as the closing brace?
I think the parser is inferring a semicolon, so it's really understanding that piece of code as
pathPrefix("hello") {
get {
respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here
complete {
<html>
<body>
<h1>Say hello to <i>spray-routing</i> on <i>spray-can</i>!</h1>
</body>
</html>
}
}
}
};
~
pathPrefix("testjson") {
get {
entity(as[TestC]) { c =>
respondWithMediaType(`application/json`) {
complete(c)
}
}
}
}
In docs :
path(x): is equivalent to rawPathPrefix(slash().concat(segment(x)).concat(pathEnd())). It matches a leading slash followed by x and then the end.
pathPrefix(x): is equivalent to rawPathPrefix(slash().concat(segment(x))). It matches a leading slash followed by x and then leaves a suffix unmatched.