Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Spring Boot と Swagger #渋谷java
Search
kariyayo
August 01, 2015
Programming
5.8k
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Spring Boot と Swagger #渋谷java
kariyayo
August 01, 2015
More Decks by kariyayo
See All by kariyayo
MobX の話
kariyayo
0
390
echoサーバーを書いてI/Oと仲良くなる話
kariyayo
3
880
SpringはどうやってDIしているのか? #jjug_ccc
kariyayo
4
3.6k
Apexで複数環境のLambda関数をデプロイする話 #jawsug
kariyayo
1
2k
近況報告といろいろ作るのが楽しい話 #yokohama_north
kariyayo
0
920
目指せ3つ星インデックス #yokohama_north
kariyayo
2
880
Gradleを使えるようになるために
kariyayo
0
120
Other Decks in Programming
See All in Programming
Go 1.27 における memory allocation の高速化
andpad
0
340
AI Readyの正体はデータマネジメントだ メダリオン2.0の最前線
freee
PRO
0
420
FastAPI の並行処理モデルを完全に理解する
hoto17296
8
3.2k
プロポーザルを書いてもらう
pvcresin
0
590
Oxlintはいいぞ(続)
yug1224
1
480
AIと壁打ちしながら進めるコスト管理
fufuhu
2
1.8k
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
1
830
komatsuna「分散システムにおけるバグ分析手法」
komatsunaqa
0
280
仕様駆動開発へのトライを機に チームに適合する手法を模索し続けている話
freee
PRO
0
650
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
780
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
570
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
460
Featured
See All Featured
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
580
How to build a perfect <img>
jonoalderson
1
5.9k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Marketing to machines
jonoalderson
1
5.7k
The SEO Collaboration Effect
kristinabergwall1
1
530
Designing for humans not robots
tammielis
254
26k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.4k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
290
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
370
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
480
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
500
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Transcript
Spring Boot ͱ Swagger 2015/8/1 ୈेೋճ #ौ୩Java bati (twitter: @bati11_)
࣍ • Swagger ͱ • SpringFox • JsonSchema • Swagger
CodeGen • σϓϩΠϝϯτύΠϓϥΠϯʹΈࠐΉ
Swagger ͱ • Web API ͷ༷Λ Swagger ͷϧʔϧʹଇͬͨ JSON Ͱهड़
• Swagger JSON ΛऔΓר֤͘छπʔϧ • ྫ͑ɺSwagger UI • σϞαΠτ http://petstore.swagger.io/
JSONͰهड़… { "swagger": "2.0", "info": { "description": "Api Documentation", "version":
"1.0", "title": "Api Documentation", "termsOfService": "urn:tos", "contact": { "name": "Contact Email" }, "license": { "name": "Apache 2.0", "url": "http://www.apache.org/licenses/LICENSE-2.0" } }, "host": "localhost:5555", "basePath": "/", "tags": [ { "name": "person-resource", "description": "Person Resource" } ], "paths": { "/persons": { "get": { "tags": [ "person-resource" ], "summary": "index", "operationId": "indexUsingGET", "consumes": [ "application/json" ], "produces": [ "application/json" ], "responses": { "200": { "description": "OK", "schema": { "type": "array", "items": { "$ref": "#/definitions/Person" }
JSON ΛखͰॻ͘ͷਏ͍ͷͰ… • Swagger Editor • ϒϥβ্Ͱಈ͘πʔϧ • YAMLɺϦΞϧλΠϜϓϨϏϡʔ •
SpringFox • Spring MVC ͳΒͪ͜Βͷબࢶ͋Δ
http://springfox.github.io/springfox/
• SpringͷΞϊςʔγϣϯΛݩʹSwagger JSON ΛੜͰ͖Δ @RestController @RequestMapping(value = "persons", produces =
MediaType.APPLICATION_JSON_VALUE) public class PersonResource { @RequestMapping(method = RequestMethod.GET) public List<Person> index() { ɾɾɾ } } SpringFox 4QSJOH#PPUͷίϯτϩʔϥʔ
SpringFoxΛ Spring Boot Ͱ͏
spring-swagger2 ΛґଘϥΠϒϥϦʹՃ repositories { ɾɾɾ jcenter() } dependencies { ɾɾɾ
compile ‘io.springfox:springfox-swagger2:2.0.3’ } CVJMEHSBEMF
Configuration ΫϥεΛ༻ҙ @EnableSwagger2 @Configuration public class SwaggerConfiguration { @Bean public
Docket customDocket() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage(“sample.web")) .build(); } }
ىಈͯ͠ /v2/api-docs.json ʹΞΫηε͢Δͱ Swagger JSON ΛऔಘͰ͖Δ $ ./gradlew bootRun $
curl http://localhost:8080/v2/api-docs.json { "swagger": "2.0", "info": { "description": "Api Documentation", "version": "1.0", "title": "Api Documentation", "termsOfService": "urn:tos", "contact": { "name": "Contact Email" }, ɾ ɾ ɾ
Swagger UI Λ Spring Boot Ͱ͏
spring-swagger-ui ΛґଘϥΠϒϥϦʹՃͯ͠ ىಈ͢Δ repositories { ɾɾɾ jcenter() } dependencies {
ɾɾɾ compile ‘io.springfox:springfox-swagger2:2.0.3’ compile 'io.springfox:springfox-swagger-ui:2.0.3' } CVJMEHSBEMF
/swagger-ui.html ʹΞΫηε͢Δͱ API υΩϡ ϝϯτ͕ݟΕΔ
SpringFox ͷ ΞϊςʔγϣϯͰ Swagger JSON ͷ ༰Λฤू͢Δ
• ϦΫΤετʹؔΘΔΞϊςʔγϣϯ • @Api, @ApiOperation, @ApiParam
• ϨεϙϯεʹؔΘΔΞϊςʔγϣϯ • @ApiModel, @ApiModelProperty
Tips
LocalDateTimeͷϓϩύςΟΛจࣈྻʹ͢Δ @Bean public Docket customDocket() { return new Docket(DocumentationType.SWAGGER_2) .select()
.apis(RequestHandlerSelectors.basePackage(“sample.web")) .build() .directModelSubstitute(LocalDateTime.class, String.class); }
OptionalͷϓϩύςΟΛจࣈྻʹ͢Δ @Bean public Docket customDocket() { return new Docket(DocumentationType.SWAGGER_2) .select()
.apis(RequestHandlerSelectors.basePackage(“sample.web")) .build() .directModelSubstitute(LocalDateTime.class, String.class) .alternateTypeRules(new OptionalTypeRule(typeResolver)); } private static class OptionalTypeRule extends AlternateTypeRule { public OptionalTypeRule(TypeResolver typeResolver) { super(typeResolver.resolve(Optional.class), typeResolver.resolve(Object.class)); } @Override public ResolvedType alternateFor(ResolvedType type) { return appliesTo(type) ? type.getTypeBindings().getTypeParameters().get(0) : type; } @Override public boolean appliesTo(ResolvedType type) { return Optional.class.isAssignableFrom(type.getErasedType()); } }
JSON Schema • Swagger JSONͷdefinitationsϓϩύςΟ JSON SchemaͰ͑Δ • http://spacetelescope.github.io/ understanding-json-schema/structuring.html
• ͚ͲɺnullʹରԠͯ͠ͳ͍ • type: [“string”, “null”] ʹ͢Δඞཁ͕͋Δ
ࣗͰඞਢ߲Ͱͳ͍ϓϩύςΟΛ type: “hoge” ͔Β type: [“hoge”, “null”] ʹ͢Δ def root
= new JsonSlurper().parseText(swaggerJson) root.definitions.entrySet().each { def required = it.value.required if (required != null) it.value.properties.each { if (!required.contains(it.key) && !it.value.containsKey('$ref')) { it.value.type = [it.value.type,"null"] } } } } def jsonBuilder = new JsonBuilder() jsonBuilder (root.definitions) definitions = jsonBuilder.toString() (SPPWZͷྫ
Swagger CodeGen • Swagger JSON ͔ΒίʔυΛࣗಈੜͰ͖Δ • ΫϥΠΞϯτଆαʔόʔଆੜͰ͖Δ • ΫϥΠΞϯτίʔυΛ༻Ͱ͖Δ͔ݕ౼த
σϓϩΠϝϯτύΠϓϥΠϯʹ ΈࠐΉ Ϗϧυ ΠϯςάϨʔγϣϯ ςετ 4XBHHFS$PEF(FO 4XBHHFS+40/ +40/4DIFNB ΫϥΠΞϯτϥΠϒϥϦ 4XBHHFS6*
͓͠·͍