找不到 Akka-HTTP 路由? [英] Akka-HTTP route not found?

查看:42
本文介绍了找不到 Akka-HTTP 路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用 Akka-Http 构建服务器,这是我目前所写的

I am learning to build server using Akka-Http and this is what I have written so far

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.stream.{Materializer, ActorMaterializer}
import spray.json.DefaultJsonProtocol
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._

import scala.concurrent.ExecutionContextExecutor


// todo() : add event timestamp
case class ECFailure(symbolicName:String)


trait Protocols extends DefaultJsonProtocol {
  implicit val ecFailureFormat = jsonFormat1(ECFailure.apply)
}

trait EmailService extends Protocols {
  implicit val system: ActorSystem
  implicit def executor: ExecutionContextExecutor
  implicit val materializer: Materializer

  val route =
    logRequestResult("email-service") {
      pathPrefix("ec") {
        (post & entity(as[ECFailure])) { ecFailure =>
          complete {
            println("received: " + ecFailure)
            "Done"
          }
        }
      }
      path("") {
        get {
          complete {
            "Welcome."
          }
        }
      }
    }
}

object ECServer extends App with EmailService {
  override implicit val system = ActorSystem("ec-server")
  override implicit val executor = system.dispatcher
  override implicit val materializer = ActorMaterializer()
  val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
}

当我运行它并点击端点时,我看到

When I run that and hit the endpoints I see

$ curl -v  http://localhost:8080
* Rebuilt URL to: http://localhost:8080/
* Hostname was NOT found in DNS cache
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
* Server akka-http/2.3.12 is not blacklisted
< Server: akka-http/2.3.12
< Date: Tue, 28 Jul 2015 21:51:04 GMT
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 8
< 
* Connection #0 to host localhost left intact
Welcome.

但另一个端点不可用

$ curl -v -XPOST -d "{'symbolicName': 'ABC'}" -H "Content-Type:application/json" http://localhost:8080/ec
* Hostname was NOT found in DNS cache
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /ec HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
> Content-Type:application/json
> Content-Length: 23
> 
* upload completely sent off: 23 out of 23 bytes
< HTTP/1.1 404 Not Found
* Server akka-http/2.3.12 is not blacklisted
< Server: akka-http/2.3.12
< Date: Tue, 28 Jul 2015 21:51:06 GMT
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 42
< 
* Connection #0 to host localhost left intact
The requested resource could not be found.

在服务器上,我看到日志为

On Server, I see logs as

Connected to the target VM, address: '127.0.0.1:58024', transport: 'socket'
[DEBUG] [07/28/2015 14:50:59.528] [main] [EventStream(akka://ec-server)] logger log1-Logging$DefaultLogger started
[DEBUG] [07/28/2015 14:50:59.530] [main] [EventStream(akka://ec-server)] Default Loggers started
[DEBUG] [07/28/2015 14:51:00.016] [ec-server-akka.actor.default-dispatcher-6] [akka://ec-server/system/IO-TCP/selectors/$a/0] Successfully bound to /127.0.0.1:8080
[DEBUG] [07/28/2015 14:51:03.917] [ec-server-akka.actor.default-dispatcher-6] [akka://ec-server/system/IO-TCP/selectors/$a/0] New connection accepted
[DEBUG] [07/28/2015 14:51:04.258] [ec-server-akka.actor.default-dispatcher-13] [ActorSystem(ec-server)] email-service: Response for
  Request : HttpRequest(HttpMethod(GET),http://localhost:8080/,List(User-Agent: curl/7.37.1, Host: localhost:8080, Accept: */*),HttpEntity.Strict(none/none,ByteString()),HttpProtocol(HTTP/1.1))
  Response: Complete(HttpResponse(200 OK,List(),HttpEntity.Strict(text/plain; charset=UTF-8,ByteString(87, 101, 108, 99, 111, 109, 101, 46)),HttpProtocol(HTTP/1.1)))
[DEBUG] [07/28/2015 14:51:06.265] [ec-server-akka.actor.default-dispatcher-19] [akka://ec-server/system/IO-TCP/selectors/$a/0] New connection accepted
[DEBUG] [07/28/2015 14:51:06.284] [ec-server-akka.actor.default-dispatcher-17] [ActorSystem(ec-server)] email-service: Response for
  Request : HttpRequest(HttpMethod(POST),http://localhost:8080/ec,List(User-Agent: curl/7.37.1, Host: localhost:8080, Accept: */*),HttpEntity.Strict(application/json,ByteString(123, 39, 115, 121, 109, 98, 111, 108, 105, 99, 78, 97, 109, 101, 39, 58, 32, 39, 65, 66, 67, 39, 125)),HttpProtocol(HTTP/1.1))
  Response: Rejected(List())

这里有什么不正确的地方?

What is not correct here?

推荐答案

不同路由之间缺少 ~ 的问题.以下修复它

The problem was missing ~ between different routes. The following fixed it

  val route =
    logRequestResult("email-service") {
      pathPrefix("ec") {
        (post & entity(as[ECFailure])) { ecFailure =>
          complete {
            println("received: " + ecFailure)
            "Done"
          }
        }
      } ~
        path("") {
          get {
            complete {
              "Welcome."
            }
          }
        }
    }

和卷曲工作

$ curl -v -XPOST -d '{"symbolicName": "ABC"}' -H "Content-Type:application/json" http://localhost:8080/ec
* Hostname was NOT found in DNS cache
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /ec HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:8080
> Accept: */*
> Content-Type:application/json
> Content-Length: 23
> 
* upload completely sent off: 23 out of 23 bytes
< HTTP/1.1 200 OK
* Server akka-http/2.3.12 is not blacklisted
< Server: akka-http/2.3.12
< Date: Tue, 28 Jul 2015 22:05:37 GMT
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 4
< 
* Connection #0 to host localhost left intact
Done

这篇关于找不到 Akka-HTTP 路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆