- 浏览: 2539559 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Playframework and Swagger
1. Prepare the RESTful Project(Upgrade)
Download the latest activator version and place in the working directory.
https://www.playframework.com/documentation/2.4.3/ScalaTestingWithScalaTest
Some Changes to Upgrade the Version
activator
declare -r app_version=“1.3.6"
activator.bat
set APP_VERSION=1.3.6
build.sbt
import play.sb.PlayScala
…snip...
scalaVersion := "2.10.4"
ivyScala := ivyScala.value map { _.copy(overrideScalaVersion = true) }
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "2.2.5" % "test",
"org.scalatestplus" %% "play" % "1.4.0-M4" % "test"
)
…snip...
project/build.properties
sbt.version=0.13.8
project/plugins.sbt
logLevel := Level.Warn
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.0")
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/"
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.3")
test/controllers/BookControllerSpec.scala
package controllers
import play.api.libs.json.JsValue
import scala.concurrent.Future
import org.scalatestplus.play._
import play.api.mvc._
import play.api.test._
import play.api.test.Helpers._
class BookControllerSpec extends PlaySpec with Results{
class TestController() extends Controller with BookController
"Book API#listBooks" should {
"size should be 2" in {
val controller = new TestController
val result: Future[Result] = controller.listBooks.apply(FakeRequest())
val bodyText: JsValue = contentAsJson(result)
bodyText must not be null
}
}
}
2. Prepare Swagger
Play with swagger-ui Project
> git clone https://github.com/swagger-api/swagger-ui.git
> npm install
> gulp
After that, we should see the distribution under the list folder.
Check if I have apache http server on my machine.
> apachectl -V
Server version: Apache/2.4.16 (Unix)
Server built: Jul 22 2015 21:03:09
Server's Module Magic Number: 20120211:47
Server loaded: APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_FLOCK_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/usr"
-D SUEXEC_BIN="/usr/bin/suexec"
-D DEFAULT_PIDLOG="/private/var/run/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="/private/etc/apache2/mime.types"
-D SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"
Start the Server
> sudo apachectl start
Check the configuration
>cat /etc/apache2/httpd.conf
DocumentRoot "/Library/WebServer/Documents"
Copy the swagger-ui directory there
> sudo cp -r /Users/carl/book/swagger/swagger-ui/dist/* /Library/WebServer/Documents/
Visit swagger-ui
http://localhost/
Put http://localhost:8000/api-docs as parameter in the input box, we will have the api doc.
Or
http://localhost/index.html?url=http://localhost:8000/api-docs
Or
http://localhost:8000/assets/lib/swagger-ui/index.html?/url=http://localhost:8000/api-docs
3. Add notation in the Project
Some Sample codes are in sillycat-scalarest project version-1.3.6-swagger branch.
build.sbt
import play.sbt.PlayScala
name := """sillycat-scalarest"""
version := "1.0"
lazy val root = project.in(file(".")).enablePlugins(PlayScala)
scalaVersion := "2.10.4"
ivyScala := ivyScala.value map { _.copy(overrideScalaVersion = true) }
libraryDependencies ++= Seq(
"pl.matisoft" %% "swagger-play24" % "1.4",
//for internal swagger-ui
//"org.reflections" % "reflections" % "0.9.9" notTransitive(),
//"org.webjars" % "swagger-ui" % "2.1.8-M1",
"org.scalatest" %% "scalatest" % "2.2.5" % "test",
"org.scalatestplus" %% "play" % "1.4.0-M4" % "test"
)
fork in run := false
conf/routes
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file)
# API Routes
GET /ping controllers.PingPongController.ping()
GET /api/v1/book controllers.BookController.listBooks
POST /api/v1/book controllers.BookController.saveBook
GET /api/v1/book/:id controllers.BookController.getBook(id:String)
PUT /api/v1/book/:id controllers.BookController.updateBook(id:String)
DELETE /api/v1/book/:id controllers.BookController.deleteBook(id:String)
# Swagger API
GET /api-docs @pl.matisoft.swagger.ApiHelpController.getResources
GET /api-docs/api/v1 @pl.matisoft.swagger.ApiHelpController.getResource(path = "/api/v1")
app/controllers/PingPongController.scala
package controllers
import play.api.mvc.{Action, Controller}
import com.wordnik.swagger.annotations.{Api,ApiModel,ApiModelProperty,ApiOperation,ApiParam,ApiResponse,ApiResponses}
@Api(value = "/api/v1", description = "Operations with Classifier")
object PingPongController extends Controller{
@ApiOperation(value = "Pings",
notes = "Returns pong",
response = classOf[String],
httpMethod = "GET",
produces = "text",
position = 1)
@ApiResponses(Array(
new ApiResponse(code = 200, message = "Successful ping", response = classOf[String])
)
)
def ping = Action {
Ok("pong")
}
}
Error Message
XMLHttpRequest cannot load http://localhost:8000/string. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 404.
Solution:
http://enable-cors.org/server.html
https://www.playframework.com/documentation/2.4.3/CorsFilter
Filter Class app/Filters.scala
import javax.inject.Inject
import play.api.http.HttpFilters
import play.filters.cors.CORSFilter
class Filters @Inject() (corsFilter: CORSFilter) extends HttpFilters {
def filters = Seq(corsFilter)
}
Configuration in conf/application.conf
play.filters.cors {
#pathPrefixes = ["/some/path", ...]
allowedOrigins = ["http://localhost", "http://sillycat.ddns.net"]
allowedHttpMethods = ["GET", "POST", "PUT"]
#allowedHttpHeaders = ["Accept"]
#preflightMaxAge = 3 days
}
4. Playframework Deployment
Build the binary
> sbt dist
Run the binary
> bin/sillycat-scalarest -Dconfig.file=conf/application.conf -Dhttp.port=8000 -Dhttp.address=0.0.0.0
Need to use the same ui version
> git checkout tags/v2.1.8-M1
References:
Playframework
http://sillycat.iteye.com/blog/2241901
http://sillycat.iteye.com/blog/2242284
Swagger
https://github.com/matiwinnetou/swagger-play24
http://swagger.io/getting-started/
https://github.com/swagger-api/swagger-ui
1. Prepare the RESTful Project(Upgrade)
Download the latest activator version and place in the working directory.
https://www.playframework.com/documentation/2.4.3/ScalaTestingWithScalaTest
Some Changes to Upgrade the Version
activator
declare -r app_version=“1.3.6"
activator.bat
set APP_VERSION=1.3.6
build.sbt
import play.sb.PlayScala
…snip...
scalaVersion := "2.10.4"
ivyScala := ivyScala.value map { _.copy(overrideScalaVersion = true) }
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "2.2.5" % "test",
"org.scalatestplus" %% "play" % "1.4.0-M4" % "test"
)
…snip...
project/build.properties
sbt.version=0.13.8
project/plugins.sbt
logLevel := Level.Warn
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.0")
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
resolvers += "Typesafe repository" at "https://repo.typesafe.com/typesafe/releases/"
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.3")
test/controllers/BookControllerSpec.scala
package controllers
import play.api.libs.json.JsValue
import scala.concurrent.Future
import org.scalatestplus.play._
import play.api.mvc._
import play.api.test._
import play.api.test.Helpers._
class BookControllerSpec extends PlaySpec with Results{
class TestController() extends Controller with BookController
"Book API#listBooks" should {
"size should be 2" in {
val controller = new TestController
val result: Future[Result] = controller.listBooks.apply(FakeRequest())
val bodyText: JsValue = contentAsJson(result)
bodyText must not be null
}
}
}
2. Prepare Swagger
Play with swagger-ui Project
> git clone https://github.com/swagger-api/swagger-ui.git
> npm install
> gulp
After that, we should see the distribution under the list folder.
Check if I have apache http server on my machine.
> apachectl -V
Server version: Apache/2.4.16 (Unix)
Server built: Jul 22 2015 21:03:09
Server's Module Magic Number: 20120211:47
Server loaded: APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_FLOCK_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/usr"
-D SUEXEC_BIN="/usr/bin/suexec"
-D DEFAULT_PIDLOG="/private/var/run/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="/private/etc/apache2/mime.types"
-D SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"
Start the Server
> sudo apachectl start
Check the configuration
>cat /etc/apache2/httpd.conf
DocumentRoot "/Library/WebServer/Documents"
Copy the swagger-ui directory there
> sudo cp -r /Users/carl/book/swagger/swagger-ui/dist/* /Library/WebServer/Documents/
Visit swagger-ui
http://localhost/
Put http://localhost:8000/api-docs as parameter in the input box, we will have the api doc.
Or
http://localhost/index.html?url=http://localhost:8000/api-docs
Or
http://localhost:8000/assets/lib/swagger-ui/index.html?/url=http://localhost:8000/api-docs
3. Add notation in the Project
Some Sample codes are in sillycat-scalarest project version-1.3.6-swagger branch.
build.sbt
import play.sbt.PlayScala
name := """sillycat-scalarest"""
version := "1.0"
lazy val root = project.in(file(".")).enablePlugins(PlayScala)
scalaVersion := "2.10.4"
ivyScala := ivyScala.value map { _.copy(overrideScalaVersion = true) }
libraryDependencies ++= Seq(
"pl.matisoft" %% "swagger-play24" % "1.4",
//for internal swagger-ui
//"org.reflections" % "reflections" % "0.9.9" notTransitive(),
//"org.webjars" % "swagger-ui" % "2.1.8-M1",
"org.scalatest" %% "scalatest" % "2.2.5" % "test",
"org.scalatestplus" %% "play" % "1.4.0-M4" % "test"
)
fork in run := false
conf/routes
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file)
# API Routes
GET /ping controllers.PingPongController.ping()
GET /api/v1/book controllers.BookController.listBooks
POST /api/v1/book controllers.BookController.saveBook
GET /api/v1/book/:id controllers.BookController.getBook(id:String)
PUT /api/v1/book/:id controllers.BookController.updateBook(id:String)
DELETE /api/v1/book/:id controllers.BookController.deleteBook(id:String)
# Swagger API
GET /api-docs @pl.matisoft.swagger.ApiHelpController.getResources
GET /api-docs/api/v1 @pl.matisoft.swagger.ApiHelpController.getResource(path = "/api/v1")
app/controllers/PingPongController.scala
package controllers
import play.api.mvc.{Action, Controller}
import com.wordnik.swagger.annotations.{Api,ApiModel,ApiModelProperty,ApiOperation,ApiParam,ApiResponse,ApiResponses}
@Api(value = "/api/v1", description = "Operations with Classifier")
object PingPongController extends Controller{
@ApiOperation(value = "Pings",
notes = "Returns pong",
response = classOf[String],
httpMethod = "GET",
produces = "text",
position = 1)
@ApiResponses(Array(
new ApiResponse(code = 200, message = "Successful ping", response = classOf[String])
)
)
def ping = Action {
Ok("pong")
}
}
Error Message
XMLHttpRequest cannot load http://localhost:8000/string. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 404.
Solution:
http://enable-cors.org/server.html
https://www.playframework.com/documentation/2.4.3/CorsFilter
Filter Class app/Filters.scala
import javax.inject.Inject
import play.api.http.HttpFilters
import play.filters.cors.CORSFilter
class Filters @Inject() (corsFilter: CORSFilter) extends HttpFilters {
def filters = Seq(corsFilter)
}
Configuration in conf/application.conf
play.filters.cors {
#pathPrefixes = ["/some/path", ...]
allowedOrigins = ["http://localhost", "http://sillycat.ddns.net"]
allowedHttpMethods = ["GET", "POST", "PUT"]
#allowedHttpHeaders = ["Accept"]
#preflightMaxAge = 3 days
}
4. Playframework Deployment
Build the binary
> sbt dist
Run the binary
> bin/sillycat-scalarest -Dconfig.file=conf/application.conf -Dhttp.port=8000 -Dhttp.address=0.0.0.0
Need to use the same ui version
> git checkout tags/v2.1.8-M1
References:
Playframework
http://sillycat.iteye.com/blog/2241901
http://sillycat.iteye.com/blog/2242284
Swagger
https://github.com/matiwinnetou/swagger-play24
http://swagger.io/getting-started/
https://github.com/swagger-api/swagger-ui
发表评论
-
Stop Update Here
2020-04-28 09:00 310I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 465NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 361Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 363Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 328Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 419Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 364Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 444VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 376Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 464NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 413Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 330Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 242GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 443GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 320GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 284Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 302Serverless with NodeJS and Tenc ...
相关推荐
在Play Framework中集成Swagger,可以极大地提升API的可访问性和可维护性。 首先,让我们深入理解Play Framework的核心特性。Play采用了模型-视图-控制器(MVC)架构模式,使得代码结构清晰,易于理解和维护。它...
在"play1.2.4集成Swagger"的场景中,我们需要将Swagger整合到Play Framework 1.2.4版本中,以便为我们的API提供强大的文档和测试功能。以下是一些关键步骤和知识点: 1. **安装Swagger**: 首先,你需要添加Swagger...
在.NET 4.5框架中,Swashbuckle仍然可以很好地工作,但请注意,随着.NET Framework的发展,一些新功能可能仅在更新的版本中可用。例如,.NET Core提供了更好的性能和跨平台兼容性,Swashbuckle也有对应的版本...
为了便于开发者和API使用者理解,Play-Utils 可能集成了 Swagger 或其他工具,自动生成清晰的 RESTful API 文档。 通过集成 Play-Utils 模块,开发者可以更加专注于业务逻辑的实现,而无需关注底层的复杂细节,...
play-swagger-ui Playframework 1.3模块可将swagger ui集成到您的应用程序中。将其添加到您的项目将依赖项放入您的dependencies.yml文件中: require: - michiruf -> swagger_ui 0.0.1定义存储库以从github获取模块...
Swagger UI和Django REST Framework的API文档生成器 完整文档: : 安装 pip install django-rest-swagger 将rest_framework_swagger添加到您的INSTALLED_APPS设置中: INSTALLED_APPS = ( ... ' rest_framework...
Swagger是一个强大的API开发工具,主要用于设计、构建、文档化和使用RESTful web服务。在这个名为"swagger2Demo"的项目中,我们看到作者利用Swagger 2创建了一个演示应用,目的是为了展示如何使用Swagger来调试接口...
Play Framework是Java和Scala的全栈Web框架,此库是Swagger与Play Framework结合的实用工具,帮助开发者在Play应用中集成Swagger的功能。 描述中的"rastajax.zip"是一个与"rastajax"相关的压缩包,它提到是一个默认...
### swagger开启身份认证 在现代Web开发中,API文档自动生成工具如Swagger变得越来越重要,它们不仅能够提高开发效率,还能够帮助团队更好地管理和维护API接口。然而,随着API暴露给外部用户,安全问题也日益突出。...
Swagger官方文档离线版是开发人员和团队在不依赖互联网连接的情况下查阅Swagger 2.0规范的重要资源。Swagger是一个流行的API开发工具,它基于OpenAPI Specification(以前称为Swagger specification),用于设计、...
Swagger 是一个流行的API开发工具,它提供了一套规范和实现,用于设计、构建、文档化和使用RESTful Web服务。在Java环境中,Swagger通常与Spring Boot框架结合使用,以简化API的开发和测试过程。本篇文章将深入探讨...
Play-Swagger插件现已重命名为 。 该版本不再处于积极开发中。 Api-First-Hand经过精心维护,除了Play 2.4支持外,还提供Play-Swagger的所有功能。 如果您想查看Play-Swagger或创建问题,请导航至。 -- -- 玩招摇 ...
springboot 2.2.7集成swagger2.9.2,并生成markdown格式API文档. <!-- swagger2 依赖开始--> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <groupId>io.springfox ...
Swagger是一个流行的API开发工具,主要用于构建、文档化和测试RESTful web服务。它提供了一种交互式的、基于HTTP的API接口,使得开发者可以轻松地理解并使用这些接口。Swagger通过JSON格式来描述API,使得服务的消费...
Swagger 是一个广泛使用的 API 设计和开发工具,它允许开发者以 YAML 或 JSON 格式定义 RESTful 风格的 Web 服务接口。这个压缩包文件 "swagger" 可能包含了 Swagger 的静态资源,这些资源主要用于展示和测试 API ...
现在市面上的swagger UI不足之处 1、原生UI显示的有些不够漂亮和清晰,特别是request 的model部分 2、每个服务都需要引入一套资源文件,不能作为一个中间件为其他API使用 3、默认通用配置繁琐,每个项目都需要复制...
Swagger UI 是一个强大的工具,用于交互式地展示和测试API接口。它基于Swagger规范,能够帮助开发者轻松地理解和使用API。在Spring框架中整合Swagger UI,可以为开发、测试和文档化RESTful服务提供极大的便利。这篇...
在Asp.Net Core中,Swagger是一个强大的工具,用于生成API文档,它可以帮助开发者轻松地创建、测试和理解API接口。本篇文章将详细讲解如何在Asp.Net Core项目中使用NSwag(包括Swashbuckle)来实现Swagger的集成。 ...
Swagger2是一个广泛使用的API文档和测试工具,它允许开发者通过注解轻松地在Java应用程序中定义RESTful API接口。这个“swagger2依赖包”包含了Swagger2实现所需的关键组件,使得开发人员可以自动化生成API的客户端...