当前位置: 首页>>代码示例>>Scala>>正文


Scala ScalatraServlet类代码示例

本文整理汇总了Scala中org.scalatra.ScalatraServlet的典型用法代码示例。如果您正苦于以下问题:Scala ScalatraServlet类的具体用法?Scala ScalatraServlet怎么用?Scala ScalatraServlet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了ScalatraServlet类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: FibonacciREST

//设置package包名称以及导入依赖的类
package com.doanduyhai.rest

import com.doanduyhai.service.FibonacciService
import org.json4s.{DefaultFormats, Formats}
import org.scalatra.{InternalServerError, BadRequest, ScalatraServlet}
import org.scalatra.json.JacksonJsonSupport

class FibonacciREST extends ScalatraServlet with JacksonJsonSupport {

  protected implicit val jsonFormats: Formats = DefaultFormats

  before() {
    contentType = formats("json")
  }

  get("/:rank") {
    val rank = params("rank").toInt
    try {
      FibonacciService.computeFibonacci(rank)
    } catch {
      case ex: IllegalArgumentException => BadRequest(ex.getMessage)
      case throwable: Throwable => InternalServerError(throwable.getMessage)
    }
  }


  notFound {
    contentType = "text/html"
    serveStaticResource() getOrElse resourceNotFound()
  }
} 
开发者ID:doanduyhai,项目名称:FibonacciScalaDemo,代码行数:32,代码来源:FibonacciREST.scala

示例2: InterceptionController

//设置package包名称以及导入依赖的类
package com.innoq.integration

import akka.actor.{ActorRef, ActorSystem}
import org.json4s.{DefaultFormats, Formats, JValue}
import org.scalatra.json.JacksonJsonSupport
import org.scalatra.{FutureSupport, Ok, ScalatraServlet}
import org.slf4j.LoggerFactory

class InterceptionController(actorSystem: ActorSystem, interceptionHandler: ActorRef) extends ScalatraServlet with JacksonJsonSupport with FutureSupport {

  protected implicit val jsonFormats: Formats = DefaultFormats

  protected override def transformRequestBody(body: JValue): JValue = body.camelizeKeys

  protected override def transformResponseBody(body: JValue): JValue = body.underscoreKeys

  protected implicit def executor = scala.concurrent.ExecutionContext.Implicits.global

  protected val log = LoggerFactory.getLogger(classOf[InterceptionController])

  log.info("Starting interception controller")

  before() {
    contentType = formats("json")
  }

  get("/*") {
    Ok
  }

  put("/") {
    val interception = parsedBody.extract[Interception]
    log.info(s"Setting interception: $interception")
    interceptionHandler ! SetInterception(interception)
    Ok
  }

  post("/") {
    val interception = parsedBody.extract[Interception]
    log.info(s"Adding interception: $interception")
    interceptionHandler ! AddInterception(interception)
    Ok
  }

  delete("/*") {
    log.info(s"Deleting interceptions")
    interceptionHandler ! DeleteInterceptions()
    Ok
  }
} 
开发者ID:mputz86,项目名称:testing-with-logging,代码行数:51,代码来源:InterceptionController.scala

示例3: LendingMessage

//设置package包名称以及导入依赖的类
package com.github.twinra.lendmanager.rest

import java.time.LocalDate

import com.github.twinra.lendmanager.dao.LendingRepository
import com.github.twinra.lendmanager.domain.Lending
import com.github.twinra.lendmanager.repo.{ItemRepository, PersonRepository}
import com.github.twinra.utils.json.JLocalDateSerializer
import com.typesafe.scalalogging.LazyLogging
import org.json4s.{DefaultFormats, Formats}
import org.scalatra.ScalatraServlet
import org.scalatra.json.JacksonJsonSupport

import scala.util.{Failure, Success, Try}

private case class LendingMessage(itemId: Long, personId: Long, date: LocalDate)

class LendingsServlet(implicit repo: LendingRepository,
                      itemsRepo: ItemRepository,
                      peopleRepo: PersonRepository) extends ScalatraServlet with LazyLogging with JacksonJsonSupport {
  protected implicit lazy val jsonFormats: Formats = DefaultFormats + JLocalDateSerializer

  before() {
    contentType = formats("json")
  }

  get("/") {
    repo.readAll()
  }

  get("/:id") {
    repo.read(params("id").toLong).getOrElse(halt(404))
  }

  post("/") {
    val lendingRequest = Try(parsedBody.extract[LendingMessage]) match {
      case Success(b) => b
      case Failure(ex) =>
        log("Failed extract lending", ex)
        halt(400)
    }
    val item = itemsRepo.read(lendingRequest.itemId).getOrElse(halt(404))
    val person = peopleRepo.read(lendingRequest.personId).getOrElse(halt(404))
    repo.add(Lending(item, person, lendingRequest.date))
  }

  delete("/:id") {
    repo.delete(params("id").toLong)
  }
} 
开发者ID:twinra,项目名称:lending-manager,代码行数:51,代码来源:LendingsServlet.scala

示例4: ItemsServlet

//设置package包名称以及导入依赖的类
package com.github.twinra.lendmanager.rest

import com.github.twinra.lendmanager.domain.Item
import com.github.twinra.lendmanager.repo.ItemRepository
import com.typesafe.scalalogging.LazyLogging
import org.json4s.{DefaultFormats, Formats}
import org.scalatra.ScalatraServlet
import org.scalatra.json.JacksonJsonSupport

class ItemsServlet(implicit val repo: ItemRepository) extends ScalatraServlet with LazyLogging with JacksonJsonSupport {
  protected implicit lazy val jsonFormats: Formats = DefaultFormats

  before() {
    contentType = formats("json")
  }

  get("/") {
    repo.readAll()
  }

  get("/:id") {
    repo.read(params("id").toLong).getOrElse(halt(404))
  }

  post("/") {
    val item = parsedBody.extract[Item]
    repo.add(item)
  }

  put("/:id") {
    val item = parsedBody.extract[Item]
    repo.update(params("id").toLong, item)
  }

  delete("/:id") {
    repo.delete(params("id").toLong)
  }
} 
开发者ID:twinra,项目名称:lending-manager,代码行数:39,代码来源:ItemsServlet.scala

示例5: PeopleServlet

//设置package包名称以及导入依赖的类
package com.github.twinra.lendmanager.rest

import com.github.twinra.lendmanager.domain.Person
import com.github.twinra.lendmanager.repo.PersonRepository
import com.typesafe.scalalogging.LazyLogging
import org.json4s.{DefaultFormats, Formats}
import org.scalatra.ScalatraServlet
import org.scalatra.json._

class PeopleServlet(implicit val repo: PersonRepository) extends ScalatraServlet with LazyLogging with JacksonJsonSupport {
  protected implicit lazy val jsonFormats: Formats = DefaultFormats

  before() {
    contentType = formats("json")
  }

  get("/") {
    repo.readAll()
  }

  get("/:id") {
    repo.read(params("id").toLong).getOrElse(halt(404))
  }

  post("/") {
    val person = parsedBody.extract[Person]
    repo.add(person)
  }

  put("/:id") {
    val person = parsedBody.extract[Person]
    repo.update(params("id").toLong, person)
  }

  delete("/:id") {
    repo.delete(params("id").toLong)
  }
} 
开发者ID:twinra,项目名称:lending-manager,代码行数:39,代码来源:PeopleServlet.scala

示例6: ScalatraBootstrap

//设置package包名称以及导入依赖的类
package jp.seraphr.fileupload.main

import javax.servlet.ServletContext

import jp.seraphr.fileupload.FileUploadApi
import jp.seraphr.fileupload.model.UploadSettings
import org.scalatra.{ LifeCycle, ScalatraServlet }
import upickle.Api


class ScalatraBootstrap extends LifeCycle {
  class FileUploadServlet extends ScalatraServlet with FileUploadApi {
    override protected val settings: UploadSettings = UploadSettings("fileparam")
    override val jsonApi: Api = upickle.default
  }

  override def init(context: ServletContext): Unit = {
    val tServlet = new FileUploadServlet
    context.mount(tServlet, "/fileupload")
  }
} 
开发者ID:seraphr,项目名称:scalatra-reactjs-fileupload,代码行数:22,代码来源:ScalatraBootstrap.scala

示例7:

//设置package包名称以及导入依赖的类
package org.akozlov.examples

import org.scalatra._
import scalate.ScalateSupport

import org.scalatra.ScalatraServlet
import org.scalatra.metrics.{MetricsSupport, HealthChecksSupport}

trait Servlet extends ScalatraServlet with ScalateSupport {
  notFound {
    // remove content type in case it was set through an action
    contentType = null
    // Try to render a ScalateTemplate if no route matched
    findTemplate(requestPath) map { path =>
      contentType = "text/html"
      layoutTemplate(path)
    } orElse serveStaticResource() getOrElse resourceNotFound()
  }
} 
开发者ID:alexvk,项目名称:ml-in-scala,代码行数:20,代码来源:Servlet.scala

示例8: LocationsServlet

//设置package包名称以及导入依赖的类
package io.foxtrot.mapeador.servlets

import io.foxtrot.mapeador.math.DistanceCalculator
import io.foxtrot.mapeador.math.clustering.DBSCAN
import io.foxtrot.mapeador.models.{TimestampedLocationsClusterFactory, Location, MapRange, TimestampedLocation}
import org.scalatra.scalate.ScalateSupport
import org.scalatra.{Params, ScalatraServlet}
import org.slf4j.LoggerFactory
import play.api.libs.json.{JsArray, JsNumber, Json}


class LocationsServlet(locations: Map[String, Seq[TimestampedLocation]]) extends ScalatraServlet with ScalateSupport {

  private val logger = LoggerFactory.getLogger(this.getClass)

  get("/") {
    contentType = "text/html"
    ssp("/index")
  }

  get("/locations") {
    contentType = "application/json"
    val mapRange = getMapRange(params).getOrElse(MapRange.world)
    Json.toJson(locations.par.map {
      case (key: String, locs: Seq[TimestampedLocation]) =>
        val rangeLocations = locs.filter(l => isInRange(l, mapRange))
        val clustered = if (rangeLocations.size > 1000) {
          DBSCAN(getClusteringMeters(mapRange), 1, includeOutliers = true, new TimestampedLocationsClusterFactory).cluster(rangeLocations).map(_.getCentroid)
        } else {
          rangeLocations
        }
        key -> JsArray(clustered.map {
          case l: TimestampedLocation =>
            JsArray(Seq(JsNumber(l.location.latitude), JsNumber(l.location.longitude)))
        }.toList)
    }.seq)
  }

  private def getMapRange(params: Params): Option[MapRange] = {
    for {
      northeast <- params.get("northeast")
      southwest <- params.get("southwest")
      neLatitude <- northeast.split(",").headOption
      neLongitude <- northeast.split(",").lastOption
      swLatitude <- southwest.split(",").headOption
      swLongitude <- southwest.split(",").lastOption
    } yield MapRange(Location(neLatitude.toDouble, neLongitude.toDouble), Location(swLatitude.toDouble, swLongitude.toDouble))
  }


  private def isInRange(timestampedLocation: TimestampedLocation, mapRange: MapRange): Boolean = {
    timestampedLocation.location.latitude < mapRange.northeast.latitude &&
    timestampedLocation.location.latitude > mapRange.southwest.latitude &&
    timestampedLocation.location.longitude < mapRange.northeast.longitude &&
    timestampedLocation.location.longitude > mapRange.southwest.longitude
  }

  private def getClusteringMeters(mapRange: MapRange): Double = {
    Math.min(DistanceCalculator.vincentyDistance(mapRange.northeast, mapRange.southwest) / 300.0, 50000)
  }
} 
开发者ID:FoxtrotSystems,项目名称:mapeador,代码行数:62,代码来源:LocationsServlet.scala

示例9: HelloWorldController

//设置package包名称以及导入依赖的类
package bdir.controllers

import org.scalatra.ScalatraServlet
import org.slf4j.LoggerFactory

class HelloWorldController extends ScalatraServlet {
  val logger = LoggerFactory.getLogger(getClass)

  get("/") {
    "Hello world"
  }

  get("/:name") {
    val name = params.getOrElse("name", "anonymous")
    "Hello " + name
  }

  post("/send") {
    val name = params.getOrElse("name", "noone")

    logger.info("Mi nombre es " + name)
  }
} 
开发者ID:ervitis,项目名称:bdir-back,代码行数:24,代码来源:HelloWorldController.scala


注:本文中的org.scalatra.ScalatraServlet类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。