generate pojo with swagger-codegen in android

generate pojo with swagger-codegen in android

swagger-codegenでpojo生成

3c4e24fd827c789cb67a9f759f057b06?s=128

Shinnosuke Kugimiya

September 29, 2017
Tweet

Transcript

  1. TXBHHFSDPEFHFOͰ QPKPੜ੒ ,HNZTIJO

  2. ࣗݾ঺հ w ఝٶͰ͢ w LHNZTIJO w ෱Ԭग़਎ w "OESPJEΤϯδχΞ

  3. 5-%3 w 4FSWFSଆ͕4XBHHFS࢖ͬͯͨΒɺϦΫΤετ΍Ϩ εϙϯεͷQPKPΛࣗಈੜ੒Ͱ͖ΔΑ w J04΋"OEPSJE΋ؔ܎ͳ͘Ͱ͖ΔΑ w ్தαϯϓϧίʔυͰ3FUPSJGUͱ͔ग़ͯ͘Δ͚Ͳɺ 3FUPSpU࢖ͬͯͳͯ͘΋େৎ෉ͩΑ

  4. ·ͣ͸͜ͷίʔυΛ

  5. public interface ApiClient { @GET("/api/users") Single<List<GetUserResponse>> getAll(); @GET("/api/users/{id}") Single<UserResponse> get(@Path("id")

    int id); @POST("/api/users") Single<UserResponse> create(@Body CreateUserRequest request); @POST("/tokens/new") Single<PublishTokenResponse> publishToken(@Body PublishTokenRequest request); @POST("/tokens/verify") Single<VerifyTokenResponse> tokenVerify(@Body VerifyTokenRequest request); }
  6. public interface ApiClient { @GET("/api/users") Single<List<GetUserResponse>> getAll(); @GET("/api/users/{id}") Single<UserResponse> get(@Path("id")

    int id); @POST("/api/users") Single<UserResponse> create(@Body CreateUserRequest request); @POST("/tokens/new") Single<PublishTokenResponse> publishToken(@Body PublishTokenRequest request); @POST("/tokens/verify") Single<VerifyTokenResponse> tokenVerify(@Body VerifyTokenRequest request); } 3FUSPpUΛ࢖༻ͯ͠ ֤"1*ͱͷૄ௨Λ ఆ͍ٛͯ͠ΔͳΜͰ΋ͳ͍ίʔυ
  7. public interface ApiClient { @GET("/api/users") Single<List<GetUserResponse>> getAll(); @GET("/api/users/{id}") Single<UserResponse> get(@Path("id")

    int id); @POST("/api/users") Single<UserResponse> create(@Body CreateUserRequest request); @POST("/tokens/new") Single<PublishTokenResponse> publishToken(@Body PublishTokenRequest request); @POST("/tokens/verify") Single<VerifyTokenResponse> tokenVerify(@Body VerifyTokenRequest request); } ͜Ε͸ಛʹ໰୊ͳ͍ ͨͩ"1*ͷ਺͕͍͢͝૿͑ͨ࣌ɺɺ
  8. public interface ApiClient { @GET("/api/users") Single<List<GetUserResponse>> getAll(); @GET("/api/users/{id}") Single<UserResponse> get(@Path("id")

    int id); @POST("/api/users") Single<UserResponse> create(@Body CreateUserRequest request); @POST("/tokens/new") Single<PublishTokenResponse> publishToken(@Body PublishTokenRequest request); @POST("/tokens/verify") Single<VerifyTokenResponse> tokenVerify(@Body VerifyTokenRequest request); : Լʹͨ͘͞Μଓ͘ } ͜ΕΒ࡞Δͷ େม͡Όͳ͍Ͱ͔͢ʁ
  9. { "item": { "id": 1234, "summary": { "name": "hoge", "description":

    "hoge" }, "items": { "items": [ { "id": 1234, "hogeId": 1234, "hogeUrl": “https://hoge.com“ } ], "pager": { "next": 3, "current": 2, "previous": 1 } } } } ྫ͑͹͜ΜͳKTPOΛ QPKPʹ͢Δͱͯ͠
  10. w UZQPඞࢸ w ৔߹ʹΑΔ͚Ͳ ωετ͞ΕͯΔ෼͚ͩΫϥε΋૿ ͑Δ w ͜͏͍͏ͷOݸ͋Δͱਏ͞΋Oݸ

  11. w UZQPඞࢸ w ৔߹ʹΑΔ͚Ͳ ωετ͞ΕͯΔ෼͚ͩΫϥε΋૿ ͑Δ w ͜͏͍͏ͷOݸ͋Δͱਏ͞΋Oݸ ໘౗ʂʂʂ

  12. w UZQPඞࢸ w ৔߹ʹΑΔ͚Ͳ ωετ͞ΕͯΔ෼͚ͩΫϥε΋૿ ͑Δ w ͜͏͍͏ͷOݸ͋Δͱਏ͞΋Oݸ TXBHHFSDPEFHFOΛ ࢖͑͹ࣗಈੜ੒Ͱ͖·͢

    લఏTFSWFSଆ͕ TXBHHFS࢖ͬͯ͘ΕͯΔ
  13. TXBHHFSDPEFHFOΛ ࢖ͬͯΈΔ

  14. ΍Δ͜ͱ w TXBHHFSDPEFHFOKBSͷμ΢ϯϩʔυ w QPKPੜ੒ͷςϯϓϨʔτ࡞੒ w TXBHHFSKTPOͷμ΢ϯϩʔυ w ੜ੒ίϚϯυଧͭ

  15. TXBHHFSDPEFHFOKBSͷμ΢ϯϩʔ υ wget http://central.maven.org/maven2/io/swagger/ swagger-codegen-cli/2.2.2/swagger-codegen- cli-2.2.2.jar -O swagger-codegen-cli.jar brew install

    swagger-codegen ԼهͰμ΢ϯϩʔυ .BDͳΒ͜ΕͰ΋0,
  16. QPKPੜ੒ͷςϯϓϨʔτ࡞੒ IUUQTHJUIVCDPNTXBHHFSBQJTXBHHFSDPEFHFOUSFFNBTUFSNPEVMFT TXBHHFSDPEFHFOTSDNBJOSFTPVSDFT ΋Ζ΋Ζͷඪ४ςϯϓϨʔτ͸Լهʹ͋Δ ࠓճ͸BOESPJEΛϕʔεʹ͍͡Δɻ QPKP͚͕ͩඞཁͳͷͰNPEFMNVTUBDIFͷΈΛ͍͡Δɻ ͦΕҎ֎͸࡟আͯ͠΋0,  ͪͳΈʹԼه͕ࣗ෼͕࡞ͬͨ΍ͭͰ͢ɻ IUUQTHJUIVCDPNLHNZTIJOBOESPJEQPKPTXBHHFSUFNQMBUF

  17. TXBHHFSKTPOͷμ΢ϯϩʔυ wget https://hoge.jp/api-docs/index -O swagger.json 4FSWFSଆ͕༻ҙ͍ͯ͠ΔTXBHHFS͔Β KTPOΛμ΢ϯϩʔυ͢Δ ͜͜ʹ͸"1*ͷ৘ใؙ͕ͬͱೖͬͯ·͢

  18. ੜ੒ίϚϯυଧͭ java -jar swagger-codegen-cli.jar generate -i ./ swagger.json -l android-poco

    -t android --model- package hoge.anroid.api --model-name-suffix Json -o dist w AJAΦϓγϣϯͰTXBHHFSKTPOࢦఆ w AUAΦϓγϣϯͰςϯϓϨʔτࢦఆ w APAΦϓγϣϯͰग़ྗઌࢦఆ w ANPEFMOBNFTV⒏YAͰQPKPͷTV⒏YΛࢦఆ w ANPEFMQBDLBHFAͰQBDLBHF໊ࢦఆ ςϯϓϨʔτʹNPEFMNVTUBDIF͔͠ͳͯ͘΋ଞͷ΋ͷ΋ੜ੒͞Ε ·͢ɻෆཁͳ΋ͷ͸࡟আͯ͠͠·͍·͠ΐ͏ɻ
  19. ͕ͭͬͱੜ੒͞Ε·͢

  20. ·ͱΊ

  21. ·ͱΊ w TFSWFSଆ͕TXBHHFS࢖ͬͯΔͳΒɺΨϯΨϯࣗಈ ੜ੒Λ