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


Scala MediaTypes类代码示例

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


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

示例1: SSServer

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

import com.services.StorageService

import akka.actor.ActorSystem
import com.support.CORSSupport
import spray.http.MediaTypes
import spray.routing.{Route, SimpleRoutingApp}

object SSServer extends App with SimpleRoutingApp with CORSSupport{


  implicit val actorSystem = ActorSystem()


  //Custom directive to replace the inclcusion of the stated return type header
  def getJson(route: Route) = get{
    respondWithMediaType(MediaTypes.`application/json`){
      route
    }
  }

  //Define Each route independently as lazy vals to keep code clean
  //Link the names of each route in the start server method

  lazy val helloRoute = get {
      cors{
        path("hello") {
          complete {
            "Welcome to the AWS Storage Service \n here are a list of the available routes:"
          }
        }
      }
  }
  
  lazy val store_pcs_processA = get {
      cors{
        path("storageServices" / "s3" / "processControllers" / "processA" / "withObject" / Segment / "andDestination" / Segment ) { (data, fileName) =>
          complete {
             StorageService.writeObjectToS3(data,fileName)
          }
        }
      }
  }
  
  
  

  startServer(interface = "localhost", port = 8084) {
    helloRoute~
    store_pcs_processA
  }

} 
开发者ID:dbdev138,项目名称:DEV-FYP-AWS-Storage-Service,代码行数:55,代码来源:SSServer.scala

示例2:

//设置package包名称以及导入依赖的类
package io.$org$.domain.$domain;format="lower"$

import io.kyriakos.library.domain.Identifiable
import spray.http.ContentTypes.`application/json`
import spray.http.{ContentTypeRange, HttpEntity, MediaTypes}
import spray.httpx.marshalling.Marshaller
import spray.httpx.unmarshalling.Unmarshaller

import scala.language.postfixOps

case class $domain$(data: String) extends Identifiable

object $domain$ {

  import spray.json._
  import DefaultJsonProtocol._

  implicit val $domain;format="lower"$Marshaller: Marshaller[$domain$] = Marshaller.of[$domain$](`application/json`) {
    ($domain;format="lower"$, contentType, marshallingContext) =>
      val jsonString: String = s"""{ "data" : "\${$domain;format="lower"$ data}" }"""
      marshallingContext marshalTo HttpEntity(contentType, jsonString)
  }

  implicit val $domain;format="lower"$Unmarshaller =
    Unmarshaller[$domain$](ContentTypeRange(MediaTypes.`application/json`)) {
      case HttpEntity.NonEmpty(contentType, content) =>
        val jsValue: JsValue = new String(content toByteArray).parseJson
        val fields: Map[String, JsValue] = jsValue.asJsObject.fields
        val data: String = fields("data").convertTo[String]
        $domain$(data)
    }

} 
开发者ID:edinhodzic,项目名称:kyriakos-rest-micro-service-spray.g8,代码行数:34,代码来源:$domain$.scala

示例3: printer

//设置package包名称以及导入依赖的类
package io.circe.spray

import cats.data.Validated
import io.circe.{ Errors, Printer, RootEncoder }
import io.circe.jawn._
import spray.http.{ ContentTypes, HttpCharsets, HttpEntity, MediaTypes }
import spray.httpx.marshalling.Marshaller
import spray.httpx.unmarshalling.Unmarshaller

trait JsonSupport {
  def printer: Printer

  implicit final def circeJsonMarshaller[A](implicit encoder: RootEncoder[A]): Marshaller[A] =
    Marshaller.delegate[A, String](ContentTypes.`application/json`) { value =>
      printer.pretty(encoder(value))
    }

  implicit def circeJsonUnmarshaller[A](implicit decoder: RootDecoder[A]): Unmarshaller[A]
}

trait FailFastUnmarshaller { this: JsonSupport =>
  implicit final def circeJsonUnmarshaller[A](implicit decoder: RootDecoder[A]): Unmarshaller[A] =
    Unmarshaller[A](MediaTypes.`application/json`) {
      case x: HttpEntity.NonEmpty =>
        decode[A](x.asString(defaultCharset = HttpCharsets.`UTF-8`))(decoder.underlying) match {
          case Right(a) => a
          case Left(e) => throw e
        }
    }
}

trait ErrorAccumulatingUnmarshaller { this: JsonSupport =>
  implicit final def circeJsonUnmarshaller[A](implicit decoder: RootDecoder[A]): Unmarshaller[A] =
    Unmarshaller[A](MediaTypes.`application/json`) {
      case x: HttpEntity.NonEmpty =>
        decodeAccumulating[A](x.asString(defaultCharset = HttpCharsets.`UTF-8`))(decoder.underlying) match {
          case Validated.Valid(result) => result
          case Validated.Invalid(errors) => throw Errors(errors)
        }
    }
}

trait NoSpacesPrinter { this: JsonSupport =>
  final def printer: Printer = Printer.noSpaces
}

final object JsonSupport extends JsonSupport with NoSpacesPrinter with FailFastUnmarshaller

final object ErrorAccumulatingJsonSupport extends JsonSupport with NoSpacesPrinter with ErrorAccumulatingUnmarshaller 
开发者ID:travisbrown,项目名称:circe-spray,代码行数:50,代码来源:JsonSupport.scala

示例4: SSServer

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

import com.services.StorageService

import akka.actor.ActorSystem
import com.support.CORSSupport
import spray.http.MediaTypes
import spray.routing.{Route, SimpleRoutingApp}

object SSServer extends App with SimpleRoutingApp with CORSSupport{


  implicit val actorSystem = ActorSystem()


  //Custom directive to replace the inclcusion of the stated return type header
  def getJson(route: Route) = get{
    respondWithMediaType(MediaTypes.`application/json`){
      route
    }
  }

  //I have defined each route independently as lazy vals to keep the code clean

  //Endpoint: List avalable endpoints
  lazy val helloRoute = get {
      cors{
        path("hello") {
          complete {
            "Welcome to the MicroDG AWS-Stroage-Service" +
            "\n Routes:" +
            "\n Store output fro, Process A: storageServices/s3/processControllers/processA/withObject/{json_object}/andDestination/{url}"
          }
        }
      }
  }
  
  //Endpoint: Write output from Process A to an S3 bucket
  lazy val store_pcs_processA = get {
      cors{
        path("storageServices" / "s3" / "processControllers" / "processA" / "withObject" / Segment / "andDestination" / Segment ) { (data, fileName) =>
          complete {
             StorageService.writeObjectToS3(data,fileName)
          }
        }
      }
  }
  
  startServer(interface = "localhost", port = 8084) {
    helloRoute~
    store_pcs_processA
  }

} 
开发者ID:microdg,项目名称:BETA-AWS-Storage-Service,代码行数:55,代码来源:SSServer.scala


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