Akka HTTP路由处理[重复]

8ulbf1ek  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(141)

此问题在此处已有答案

Akka-HTTP route not found?(1个答案)
去年关闭了。
我有以下路线设置

val routes = protectedRoute("wallet") { user =>
    concat {
      path("wallet" / "deposit") {
        get {
          completeEitherT(walletService.deposit(user._id, "dollars", 50))
        }
      }
      path("wallet") {
        get {
          completeEitherT(walletService.getForUser(user._id))
        }
      }
    }
  }

completeEitherT函数如下所示

def completeEitherT[T](t: EitherT[Future, DatabaseError, T])(implicit marsh: ToResponseMarshaller[T]): Route = {
    onSuccess(t.value) {
      case Left(x: DatabaseErrors.NotFound) =>
        logger.error(x.toString)
        complete(StatusCodes.NotFound)
      case Left(error) => complete(StatusCodes.InternalServerError)
      case Right(value) => complete(value)
    }
  }

protectedRoute指令为:

def protectedRoute(permissions: String*): Directive1[User] = new Directive1[User] {
    override def tapply(f: (Tuple1[User]) => Route): Route = {
      headerValueByName("Authorization") { jwt =>
        Jwt.decode(jwt, jwtSecret, List(algo)) match {
          case Failure(_) =>
            logger.error("Unauthorized access from jwtToken:", jwt)
            complete(StatusCodes.Unauthorized)
          case Success(value) =>
            val user = JsonParser(value.content).convertTo[User]
            if (user.roles.flatMap(_.permissions).exists(permissions.contains)) f(Tuple1(user))
            else complete(StatusCodes.Forbidden)
        }
      }
    }
  }

我遇到的问题是,无论先定义哪条路径,当通过Postman调用时都会导致404。

The requested resource could not be found.

这不是来自completeEitherT的响应,因为它不记录错误
如何在同一个指令中定义两个或多个路径?

o7jaxewo

o7jaxewo1#

请标记为重复,我找不到这在谷歌搜索,但因此显示这是相关的。
Answer
实际上,路径之间缺少一个~

val routes = protectedRoute("wallet") { user =>
    concat {
      path("wallet" / "deposit") {
        get {
          completeEitherT(walletService.deposit(user._id, "dollars", 50))
        }
      } ~
      path("wallet") {
        get {
          completeEitherT(walletService.getForUser(user._id))
        }
      }
    }
  }

相关问题