Hello Spary

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.sparyTest
import akka.actor.Actor
import 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.sparyTest

import akka.actor.ActorSystem
import akka.actor.Props
import akka.util.Timeout
import akka.io.IO
import spray.can.Http
import scala.concurrent.duration._
import akka.pattern.ask

object 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

然后就可以看到响应了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值