Hello Spary

来源:互联网 发布:批处理 telnet 端口 编辑:程序博客网 时间:2024/05/17 02:54

spray是什么:spary是使用scala编写的高效的rest框架,使用起来很简单,上手比较快
下面开始入正题:
引入依赖:(我使用的maven,应为sbt一直用不顺,依赖一直下不下来)

  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.12</version>      <scope>test</scope>    </dependency>    <!-- https://mvnrepository.com/artifact/io.spray/spray-can_2.11 -->    <dependency>        <groupId>io.spray</groupId>        <artifactId>spray-can_2.10</artifactId>        <version>1.3.3</version>    </dependency>    <!-- https://mvnrepository.com/artifact/io.spray/spray-routing_2.11 -->    <dependency>        <groupId>io.spray</groupId>        <artifactId>spray-routing_2.10</artifactId>        <version>1.3.3</version>    </dependency>    <!-- https://mvnrepository.com/artifact/io.spray/spray-testkit_2.11 -->    <dependency>        <groupId>io.spray</groupId>        <artifactId>spray-testkit_2.10</artifactId>        <version>1.3.3</version>        <scope>test</scope>    </dependency>    <!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-actor_2.11 -->    <dependency>        <groupId>com.typesafe.akka</groupId>        <artifactId>akka-actor_2.10</artifactId>        <version>2.3.9</version>    </dependency>    <!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-testkit_2.11 -->    <dependency>        <groupId>com.typesafe.akka</groupId>        <artifactId>akka-testkit_2.10</artifactId>        <version>2.3.9</version>        <scope>test</scope>    </dependency>    <!-- https://mvnrepository.com/artifact/org.specs2/specs2-core_2.11 -->    <dependency>        <groupId>org.specs2</groupId>        <artifactId>specs2-core_2.10</artifactId>        <version>2.3.11</version>        <scope>test</scope>    </dependency>    <!-- https://mvnrepository.com/artifact/org.json4s/json4s-native_2.12 -->    <dependency>        <groupId>org.json4s</groupId>        <artifactId>json4s-native_2.10</artifactId>        <version>3.2.11</version>    </dependency>    <!-- https://mvnrepository.com/artifact/com.github.scopt/scopt_2.11 -->    <dependency>        <groupId>com.github.scopt</groupId>        <artifactId>scopt_2.10</artifactId>        <version>3.3.0</version>    </dependency>   </dependencies>

编写一个service

package com.git.sparyTestimport akka.actor.Actorimport spray.routing._import spray.http._import MediaTypes._class MyServiceActor extends Actor with MyService {  def actorRefFactory = context  def receive = runRoute(myRoute)}trait MyService extends HttpService {  val myRoute =    path("") {      get {        respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default, so we simply override here          complete {            <html>              <body>                <h1>Say hello to xiaopengpeng !</h1>              </body>            </html>          }        }      }    }}

编写一个启动类:(spary自带的有微容器,也可以跟tomcat,jetty等容器结合使用,这里我使用的是spary-can自带的容器)

package com.git.sparyTestimport akka.actor.ActorSystemimport akka.actor.Propsimport akka.util.Timeoutimport akka.io.IOimport spray.can.Httpimport scala.concurrent.duration._import akka.pattern.askobject Boot extends App{  implicit val system = ActorSystem("on-spray-can")  val service = system.actorOf(Props[MyServiceActor],"demo-service")  implicit val timeout = Timeout(5)  IO(Http)?Http.Bind(service,interface= "localhost",port = 8080)}

编码完成,然后直接启动Boot类,就行运行一个main程序一样,运行没有报错的情况下就可以在浏览器上面输入

localhost:8080

然后就可以看到响应了

原创粉丝点击