swagger combine some repeating annotations










3














I use .useDefaultResponseMessages(false) and in each method



@ApiResponses(value = 
@ApiResponse(code = 200, message = "Success", response = Order.class),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Failure"))
public Order getOrder......

@ApiResponses(value =
@ApiResponse(code = 200, message = "Success", response = User.class),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden"),
@ApiResponse(code = 404, message = "Not Found"),
@ApiResponse(code = 500, message = "Failure"))
public User getUser......


Can I combine some repeating annotations?(default)










share|improve this question


























    3














    I use .useDefaultResponseMessages(false) and in each method



    @ApiResponses(value = 
    @ApiResponse(code = 200, message = "Success", response = Order.class),
    @ApiResponse(code = 401, message = "Unauthorized"),
    @ApiResponse(code = 403, message = "Forbidden"),
    @ApiResponse(code = 404, message = "Not Found"),
    @ApiResponse(code = 500, message = "Failure"))
    public Order getOrder......

    @ApiResponses(value =
    @ApiResponse(code = 200, message = "Success", response = User.class),
    @ApiResponse(code = 401, message = "Unauthorized"),
    @ApiResponse(code = 403, message = "Forbidden"),
    @ApiResponse(code = 404, message = "Not Found"),
    @ApiResponse(code = 500, message = "Failure"))
    public User getUser......


    Can I combine some repeating annotations?(default)










    share|improve this question
























      3












      3








      3


      2





      I use .useDefaultResponseMessages(false) and in each method



      @ApiResponses(value = 
      @ApiResponse(code = 200, message = "Success", response = Order.class),
      @ApiResponse(code = 401, message = "Unauthorized"),
      @ApiResponse(code = 403, message = "Forbidden"),
      @ApiResponse(code = 404, message = "Not Found"),
      @ApiResponse(code = 500, message = "Failure"))
      public Order getOrder......

      @ApiResponses(value =
      @ApiResponse(code = 200, message = "Success", response = User.class),
      @ApiResponse(code = 401, message = "Unauthorized"),
      @ApiResponse(code = 403, message = "Forbidden"),
      @ApiResponse(code = 404, message = "Not Found"),
      @ApiResponse(code = 500, message = "Failure"))
      public User getUser......


      Can I combine some repeating annotations?(default)










      share|improve this question













      I use .useDefaultResponseMessages(false) and in each method



      @ApiResponses(value = 
      @ApiResponse(code = 200, message = "Success", response = Order.class),
      @ApiResponse(code = 401, message = "Unauthorized"),
      @ApiResponse(code = 403, message = "Forbidden"),
      @ApiResponse(code = 404, message = "Not Found"),
      @ApiResponse(code = 500, message = "Failure"))
      public Order getOrder......

      @ApiResponses(value =
      @ApiResponse(code = 200, message = "Success", response = User.class),
      @ApiResponse(code = 401, message = "Unauthorized"),
      @ApiResponse(code = 403, message = "Forbidden"),
      @ApiResponse(code = 404, message = "Not Found"),
      @ApiResponse(code = 500, message = "Failure"))
      public User getUser......


      Can I combine some repeating annotations?(default)







      spring swagger






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 20 '17 at 14:14









      user5620472

      93721947




      93721947






















          1 Answer
          1






          active

          oldest

          votes


















          0














          1. Use @ApiResponses at controller level.



          Define common responses at the controller level instead of repeating them for each method:



          @ApiResponses(
          @ApiResponse(code = 401, message = "Unauthorized"),
          @ApiResponse(code = 403, message = "Forbidden"),
          @ApiResponse(code = 404, message = "Not Found"),
          @ApiResponse(code = 500, message = "Failure"))
          @Controller
          public class UserOrderController
          @ApiResponse(code = 200, message = "Success", response = Order.class)
          @GetMapping("/order")
          public Order getOrder() /*......*/

          @ApiResponse(code = 200, message = "Success", response = User.class)
          @GetMapping("/user")
          public User getUser() /*......*/



          And actually, it's not required to specify response type in @ApiResponse if it matches method return type. So, in the provided example, we can define all responses once per controller in order to reduce repeating annotations.



          2. Use custom annotations



          Share the repeating annotations between controllers by defining a custom annotation:



          /**
          * A convenient meta-annotation for Swagger API responses.
          */
          @Target(ElementType.METHOD, ElementType.TYPE)
          @Retention(RetentionPolicy.RUNTIME)
          @ApiResponses(
          @ApiResponse(code = 200, message = "Success"),
          @ApiResponse(code = 401, message = "Unauthorized"),
          @ApiResponse(code = 403, message = "Forbidden"),
          @ApiResponse(code = 404, message = "Not Found"),
          @ApiResponse(code = 500, message = "Failure"))
          @interface DefaultApiResponses


          Then use it like this:



          @DefaultApiResponses
          @Controller
          public class UserOrderController
          @GetMapping("/order")
          public Order getOrder() /*......*/

          @GetMapping("/user")
          public User getUser() /*......*/



          The source code presented in this answer is available over on Github.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46324478%2fswagger-combine-some-repeating-annotations%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            1. Use @ApiResponses at controller level.



            Define common responses at the controller level instead of repeating them for each method:



            @ApiResponses(
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Forbidden"),
            @ApiResponse(code = 404, message = "Not Found"),
            @ApiResponse(code = 500, message = "Failure"))
            @Controller
            public class UserOrderController
            @ApiResponse(code = 200, message = "Success", response = Order.class)
            @GetMapping("/order")
            public Order getOrder() /*......*/

            @ApiResponse(code = 200, message = "Success", response = User.class)
            @GetMapping("/user")
            public User getUser() /*......*/



            And actually, it's not required to specify response type in @ApiResponse if it matches method return type. So, in the provided example, we can define all responses once per controller in order to reduce repeating annotations.



            2. Use custom annotations



            Share the repeating annotations between controllers by defining a custom annotation:



            /**
            * A convenient meta-annotation for Swagger API responses.
            */
            @Target(ElementType.METHOD, ElementType.TYPE)
            @Retention(RetentionPolicy.RUNTIME)
            @ApiResponses(
            @ApiResponse(code = 200, message = "Success"),
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Forbidden"),
            @ApiResponse(code = 404, message = "Not Found"),
            @ApiResponse(code = 500, message = "Failure"))
            @interface DefaultApiResponses


            Then use it like this:



            @DefaultApiResponses
            @Controller
            public class UserOrderController
            @GetMapping("/order")
            public Order getOrder() /*......*/

            @GetMapping("/user")
            public User getUser() /*......*/



            The source code presented in this answer is available over on Github.






            share|improve this answer



























              0














              1. Use @ApiResponses at controller level.



              Define common responses at the controller level instead of repeating them for each method:



              @ApiResponses(
              @ApiResponse(code = 401, message = "Unauthorized"),
              @ApiResponse(code = 403, message = "Forbidden"),
              @ApiResponse(code = 404, message = "Not Found"),
              @ApiResponse(code = 500, message = "Failure"))
              @Controller
              public class UserOrderController
              @ApiResponse(code = 200, message = "Success", response = Order.class)
              @GetMapping("/order")
              public Order getOrder() /*......*/

              @ApiResponse(code = 200, message = "Success", response = User.class)
              @GetMapping("/user")
              public User getUser() /*......*/



              And actually, it's not required to specify response type in @ApiResponse if it matches method return type. So, in the provided example, we can define all responses once per controller in order to reduce repeating annotations.



              2. Use custom annotations



              Share the repeating annotations between controllers by defining a custom annotation:



              /**
              * A convenient meta-annotation for Swagger API responses.
              */
              @Target(ElementType.METHOD, ElementType.TYPE)
              @Retention(RetentionPolicy.RUNTIME)
              @ApiResponses(
              @ApiResponse(code = 200, message = "Success"),
              @ApiResponse(code = 401, message = "Unauthorized"),
              @ApiResponse(code = 403, message = "Forbidden"),
              @ApiResponse(code = 404, message = "Not Found"),
              @ApiResponse(code = 500, message = "Failure"))
              @interface DefaultApiResponses


              Then use it like this:



              @DefaultApiResponses
              @Controller
              public class UserOrderController
              @GetMapping("/order")
              public Order getOrder() /*......*/

              @GetMapping("/user")
              public User getUser() /*......*/



              The source code presented in this answer is available over on Github.






              share|improve this answer

























                0












                0








                0






                1. Use @ApiResponses at controller level.



                Define common responses at the controller level instead of repeating them for each method:



                @ApiResponses(
                @ApiResponse(code = 401, message = "Unauthorized"),
                @ApiResponse(code = 403, message = "Forbidden"),
                @ApiResponse(code = 404, message = "Not Found"),
                @ApiResponse(code = 500, message = "Failure"))
                @Controller
                public class UserOrderController
                @ApiResponse(code = 200, message = "Success", response = Order.class)
                @GetMapping("/order")
                public Order getOrder() /*......*/

                @ApiResponse(code = 200, message = "Success", response = User.class)
                @GetMapping("/user")
                public User getUser() /*......*/



                And actually, it's not required to specify response type in @ApiResponse if it matches method return type. So, in the provided example, we can define all responses once per controller in order to reduce repeating annotations.



                2. Use custom annotations



                Share the repeating annotations between controllers by defining a custom annotation:



                /**
                * A convenient meta-annotation for Swagger API responses.
                */
                @Target(ElementType.METHOD, ElementType.TYPE)
                @Retention(RetentionPolicy.RUNTIME)
                @ApiResponses(
                @ApiResponse(code = 200, message = "Success"),
                @ApiResponse(code = 401, message = "Unauthorized"),
                @ApiResponse(code = 403, message = "Forbidden"),
                @ApiResponse(code = 404, message = "Not Found"),
                @ApiResponse(code = 500, message = "Failure"))
                @interface DefaultApiResponses


                Then use it like this:



                @DefaultApiResponses
                @Controller
                public class UserOrderController
                @GetMapping("/order")
                public Order getOrder() /*......*/

                @GetMapping("/user")
                public User getUser() /*......*/



                The source code presented in this answer is available over on Github.






                share|improve this answer














                1. Use @ApiResponses at controller level.



                Define common responses at the controller level instead of repeating them for each method:



                @ApiResponses(
                @ApiResponse(code = 401, message = "Unauthorized"),
                @ApiResponse(code = 403, message = "Forbidden"),
                @ApiResponse(code = 404, message = "Not Found"),
                @ApiResponse(code = 500, message = "Failure"))
                @Controller
                public class UserOrderController
                @ApiResponse(code = 200, message = "Success", response = Order.class)
                @GetMapping("/order")
                public Order getOrder() /*......*/

                @ApiResponse(code = 200, message = "Success", response = User.class)
                @GetMapping("/user")
                public User getUser() /*......*/



                And actually, it's not required to specify response type in @ApiResponse if it matches method return type. So, in the provided example, we can define all responses once per controller in order to reduce repeating annotations.



                2. Use custom annotations



                Share the repeating annotations between controllers by defining a custom annotation:



                /**
                * A convenient meta-annotation for Swagger API responses.
                */
                @Target(ElementType.METHOD, ElementType.TYPE)
                @Retention(RetentionPolicy.RUNTIME)
                @ApiResponses(
                @ApiResponse(code = 200, message = "Success"),
                @ApiResponse(code = 401, message = "Unauthorized"),
                @ApiResponse(code = 403, message = "Forbidden"),
                @ApiResponse(code = 404, message = "Not Found"),
                @ApiResponse(code = 500, message = "Failure"))
                @interface DefaultApiResponses


                Then use it like this:



                @DefaultApiResponses
                @Controller
                public class UserOrderController
                @GetMapping("/order")
                public Order getOrder() /*......*/

                @GetMapping("/user")
                public User getUser() /*......*/



                The source code presented in this answer is available over on Github.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 '18 at 20:17

























                answered Nov 12 '18 at 17:00









                naXa

                13.2k887132




                13.2k887132



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46324478%2fswagger-combine-some-repeating-annotations%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                    Museum of Modern and Contemporary Art of Trento and Rovereto