admin管理员组文章数量:1023127
I am building an API to track student data using Scala, Akka HTTP, and Akka Actors.
But the API Routes below are not giving me the desired result.
The routes littlerock/admins
and littlerock/schools/1
work as expected, but the rest show the error The requested resource could not be found
.
I want the API to look for example like below.
All district admins - GET: http://localhost:8080/littlerock/admins
A school's details - GET: http://localhost:8080/littlerock/schools/{1}
All a school's students - GET: http://localhost:8080/littlerock/schools/{1}/students
A student's details - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}
All the student's classmates - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}/classmates
A student's classmate details - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}/classmates/{1}
Could you help me fix the issue!
Thanks in advance!
The Routes code looks like below:
val schoolRoutes: Route =
pathPrefix("littlerock") {
concat(
pathPrefix("admins") {
concat(
pathEnd {
concat(
get {
complete("District administrators.")
})
})
},
pathPrefix("schools") {
concat(
path(Segment) { name =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A school's details.")
}
}
},
path("students") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("All students")
}
}
},
path(Segment) { name =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's details")
}
}},
path("classmates") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's classmates.")
}
}
},
path(Segment) { name =>
concat(
get {
rejectEmptyResponse {
complete("A classmate's details.")
}
})
}
)
}
)
}
)
}
)
})
}
)}
I am building an API to track student data using Scala, Akka HTTP, and Akka Actors.
But the API Routes below are not giving me the desired result.
The routes littlerock/admins
and littlerock/schools/1
work as expected, but the rest show the error The requested resource could not be found
.
I want the API to look for example like below.
All district admins - GET: http://localhost:8080/littlerock/admins
A school's details - GET: http://localhost:8080/littlerock/schools/{1}
All a school's students - GET: http://localhost:8080/littlerock/schools/{1}/students
A student's details - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}
All the student's classmates - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}/classmates
A student's classmate details - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}/classmates/{1}
Could you help me fix the issue!
Thanks in advance!
The Routes code looks like below:
val schoolRoutes: Route =
pathPrefix("littlerock") {
concat(
pathPrefix("admins") {
concat(
pathEnd {
concat(
get {
complete("District administrators.")
})
})
},
pathPrefix("schools") {
concat(
path(Segment) { name =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A school's details.")
}
}
},
path("students") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("All students")
}
}
},
path(Segment) { name =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's details")
}
}},
path("classmates") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's classmates.")
}
}
},
path(Segment) { name =>
concat(
get {
rejectEmptyResponse {
complete("A classmate's details.")
}
})
}
)
}
)
}
)
}
)
})
}
)}
Share
Improve this question
asked Nov 19, 2024 at 14:19
VakinduVakindu
8351 gold badge10 silver badges26 bronze badges
1
|
2 Answers
Reset to default 1The reason is that path(...)
matches against the full request path left, but you are using it to try to match prefixes of path, if you use pathPrefix(...)
instead in the places where you do not want to match the full path up to the end that should sort it.
Like @johanandren suggested, I replaced all except the last path
with pathPrefix
.
And the code now works!
The Routes code now looks like below:
val schoolRoutes: Route =
pathPrefix("littlerock") {
concat(
pathPrefix("admins") {
concat(
pathEnd {
concat(
get {
complete("District administrators.")
})
})
},
pathPrefix("schools") {
concat(
pathPrefix(Segment) { name =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A school's details.")
}
}
},
pathPrefix("students") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("All students")
}
}
},
pathPrefix(Segment) { studentName =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's details")
}
}},
pathPrefix("classmates") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's classmates.")
}
}
},
path(Segment) { classmateName =>
concat(
get {
rejectEmptyResponse {
complete("A classmate's details.")
}
})
}
)
}
)
}
)
}
)
})
}
)}
I am building an API to track student data using Scala, Akka HTTP, and Akka Actors.
But the API Routes below are not giving me the desired result.
The routes littlerock/admins
and littlerock/schools/1
work as expected, but the rest show the error The requested resource could not be found
.
I want the API to look for example like below.
All district admins - GET: http://localhost:8080/littlerock/admins
A school's details - GET: http://localhost:8080/littlerock/schools/{1}
All a school's students - GET: http://localhost:8080/littlerock/schools/{1}/students
A student's details - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}
All the student's classmates - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}/classmates
A student's classmate details - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}/classmates/{1}
Could you help me fix the issue!
Thanks in advance!
The Routes code looks like below:
val schoolRoutes: Route =
pathPrefix("littlerock") {
concat(
pathPrefix("admins") {
concat(
pathEnd {
concat(
get {
complete("District administrators.")
})
})
},
pathPrefix("schools") {
concat(
path(Segment) { name =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A school's details.")
}
}
},
path("students") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("All students")
}
}
},
path(Segment) { name =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's details")
}
}},
path("classmates") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's classmates.")
}
}
},
path(Segment) { name =>
concat(
get {
rejectEmptyResponse {
complete("A classmate's details.")
}
})
}
)
}
)
}
)
}
)
})
}
)}
I am building an API to track student data using Scala, Akka HTTP, and Akka Actors.
But the API Routes below are not giving me the desired result.
The routes littlerock/admins
and littlerock/schools/1
work as expected, but the rest show the error The requested resource could not be found
.
I want the API to look for example like below.
All district admins - GET: http://localhost:8080/littlerock/admins
A school's details - GET: http://localhost:8080/littlerock/schools/{1}
All a school's students - GET: http://localhost:8080/littlerock/schools/{1}/students
A student's details - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}
All the student's classmates - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}/classmates
A student's classmate details - GET: http://localhost:8080/littlerock/schools/{1}/students/{1}/classmates/{1}
Could you help me fix the issue!
Thanks in advance!
The Routes code looks like below:
val schoolRoutes: Route =
pathPrefix("littlerock") {
concat(
pathPrefix("admins") {
concat(
pathEnd {
concat(
get {
complete("District administrators.")
})
})
},
pathPrefix("schools") {
concat(
path(Segment) { name =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A school's details.")
}
}
},
path("students") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("All students")
}
}
},
path(Segment) { name =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's details")
}
}},
path("classmates") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's classmates.")
}
}
},
path(Segment) { name =>
concat(
get {
rejectEmptyResponse {
complete("A classmate's details.")
}
})
}
)
}
)
}
)
}
)
})
}
)}
Share
Improve this question
asked Nov 19, 2024 at 14:19
VakinduVakindu
8351 gold badge10 silver badges26 bronze badges
1
-
2
Look's good at first. Unless you've got a typo. I'd recommend to minimize the problem: write the minimum code required for
littlerock/schools/{1}/students
to work for instance and see what it does. – Gaël J Commented Nov 19, 2024 at 18:44
2 Answers
Reset to default 1The reason is that path(...)
matches against the full request path left, but you are using it to try to match prefixes of path, if you use pathPrefix(...)
instead in the places where you do not want to match the full path up to the end that should sort it.
Like @johanandren suggested, I replaced all except the last path
with pathPrefix
.
And the code now works!
The Routes code now looks like below:
val schoolRoutes: Route =
pathPrefix("littlerock") {
concat(
pathPrefix("admins") {
concat(
pathEnd {
concat(
get {
complete("District administrators.")
})
})
},
pathPrefix("schools") {
concat(
pathPrefix(Segment) { name =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A school's details.")
}
}
},
pathPrefix("students") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("All students")
}
}
},
pathPrefix(Segment) { studentName =>
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's details")
}
}},
pathPrefix("classmates") {
concat(
pathEnd {
get {
rejectEmptyResponse {
complete("A student's classmates.")
}
}
},
path(Segment) { classmateName =>
concat(
get {
rejectEmptyResponse {
complete("A classmate's details.")
}
})
}
)
}
)
}
)
}
)
})
}
)}
本文标签: Scala Akka HTTP problem adding multiple segment variables to API URL RoutesStack Overflow
版权声明:本文标题:Scala Akka HTTP problem adding multiple segment variables to API URL Routes - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745555222a2155834.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
littlerock/schools/{1}/students
to work for instance and see what it does. – Gaël J Commented Nov 19, 2024 at 18:44