• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala MediaTypes类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala AggregateEvent类代码示例发布时间:2022-05-23
下一篇:
Scala OptionalSourceMapper类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap