Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring Boot と Swagger #渋谷java

kariyayo
August 01, 2015

Spring Boot と Swagger #渋谷java

kariyayo

August 01, 2015
Tweet

More Decks by kariyayo

Other Decks in Programming

Transcript

  1. ໨࣍ • Swagger ͱ͸ • SpringFox • JsonSchema • Swagger

    CodeGen • σϓϩΠϝϯτύΠϓϥΠϯʹ૊ΈࠐΉ
  2. Swagger ͱ͸ • Web API ͷ࢓༷Λ Swagger ͷϧʔϧʹଇͬͨ JSON Ͱهड़

    • Swagger JSON ΛऔΓר֤͘छπʔϧ • ྫ͑͹ɺSwagger UI • σϞαΠτ http://petstore.swagger.io/
  3. 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" }
  4. • 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ͷίϯτϩʔϥʔ
  5. spring-swagger2 ΛґଘϥΠϒϥϦʹ௥Ճ repositories { ɾɾɾ jcenter() } dependencies { ɾɾɾ

    compile ‘io.springfox:springfox-swagger2:2.0.3’ } CVJMEHSBEMF
  6. Configuration ΫϥεΛ༻ҙ @EnableSwagger2 @Configuration public class SwaggerConfiguration { @Bean public

    Docket customDocket() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage(“sample.web")) .build(); } }
  7. ىಈͯ͠ /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" }, ɾ ɾ ɾ
  8. spring-swagger-ui ΛґଘϥΠϒϥϦʹ௥Ճͯ͠ ىಈ͢Δ repositories { ɾɾɾ jcenter() } dependencies {

    ɾɾɾ compile ‘io.springfox:springfox-swagger2:2.0.3’ compile 'io.springfox:springfox-swagger-ui:2.0.3' } CVJMEHSBEMF
  9. LocalDateTimeͷϓϩύςΟΛจࣈྻʹ͢Δ @Bean public Docket customDocket() { return new Docket(DocumentationType.SWAGGER_2) .select()

    .apis(RequestHandlerSelectors.basePackage(“sample.web")) .build() .directModelSubstitute(LocalDateTime.class, String.class); }
  10. 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()); } }
  11. ࣗ෼Ͱඞਢ߲໨Ͱͳ͍ϓϩύςΟΛ 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ͷྫ