Multiple Depth Spraying

I have 4 levels of URL based response. So for:
* GET to /abc answer should be abc
* GET on /abc/def answer should be def
* GET to /abc/def/ghi answer should be ghi
* GET on /abc/def/ghi/jkl answer should be (surprisingly) jkl

So my question is how to create such a request-response property using Spray.io?

I know that using pathPrefix with path possible, but is it only for two levels? With this approach, it should look something like this:

 val route = { path("/abc") { // complete with response 'abc' } ~ path("/abc/def") { // complete with response 'def' } ~ path("/abc/def/ghi") { // complete with response 'ghi' } ~ path("/abc/def/ghi/jkl") { // complete with response 'jkl' } } 

Is there a way to "nest paths"? I know this doesn't work, but just an idea like:

 val route = { path("/abc") { // complete with response 'abc' 'sub'path("/def") { // complete with response 'def' 'sub'path("/ghi") { // complete with response 'ghi' 'sub'path("/jkl") { // complete with response 'jkl' } } } } } 

Or any other correct way of nesting paths?

Best, zmeda

+6
source share
1 answer

I think the question is how to do both, complementing something on the same level if the path is complete or go one level deeper if there is still a path available.

Try the following:

 val route = { pathPrefix("abc") { // complete with response 'abc' pathEnd { complete("abc") } ~ // don't forget the twiggles pathPrefix("def") { pathEnd { complete("def") } ~ pathPrefix("ghi") { pathEnd { complete("ghi") } ~ path("jkl") { // path already includes pathEnd, path(x) = pathPrefix(x ~ PathEnd) complete("jkl") } } } } } 

Btw. this does not match "/ abc /", etc., that is, a path with a trailing slash. If you want to match trailing slashes, it is mandatory or optional to use pathSingleSlash or pathEndOrSingleSlash instead of pathEnd .

+12
source

All Articles