scala – Spray:routing – 了解path和pathPrefix之间的区别
发布时间:2020-12-16 18:52:08 所属栏目:安全 来源:网络整理
导读:import akka.actor.Actorimport spray.routing.HttpServiceimport spray.http._import MediaTypes._import spray.json._import spray.routing.directives.CachingDirectives._import spray.httpx.encoding._trait MarginEvaluationService extends HttpServi
import akka.actor.Actor import spray.routing.HttpService import spray.http._ import MediaTypes._ import spray.json._ import spray.routing.directives.CachingDirectives._ import spray.httpx.encoding._ trait MarginEvaluationService extends HttpService { import ClassSerializer._ import spray.httpx.SprayJsonSupport._ val myRoute = { 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) } } } } } } route被窃听:
path和 解决方法
path是最终路径,而pathPrefix可以随后使用DSL与其他路径段组合.
如果你想完全匹配/你好,你应该使用路径(“你好”). pathPrefix在类似的情况下很方便 pathPrefix("hello") { path("foo") { complete("foo") } ~ path("bar") { complete("bar") } } 这将匹配/ hello / foo和/ hello / bar. 话虽如此,我怀疑你得到的错误只是scala解析器与DSL不相处. 你可以尝试在与闭合支架相同的线上移动?吗? 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) } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |