Swagger Custom Annotation

1,373 views
Skip to first unread message

Puneet Jain

unread,
Dec 15, 2014, 6:11:36 PM12/15/14
to swagger-sw...@googlegroups.com
Hi,

I am using swagger for my rest api. I have a lot of few common responses for all my rest api, for example every api returns following responses:

  @ApiResponses(Array(
    new ApiResponse(code = 200, message = "Api response"),
    new ApiResponse(code = 400, message = "Bad Request - Invalid id"),
    new ApiResponse(code = 400, message = "Bad Request - Illegal Argument Exception"),
    new ApiResponse(code = 400, message = "Bad Request - Parsing Exception"),
    new ApiResponse(code = 403, message = "Forbidden - invalid token"),
    new ApiResponse(code = 500, message = "Internal server error")))

Is there any way I have pick these response from a common place, instead of defined in each and every api?
Or I can write my own custom swagger annotation, and hard code these responses?

Thanks in advance

-Puneet

Ron

unread,
Dec 16, 2014, 3:11:15 AM12/16/14
to swagger-sw...@googlegroups.com
Assuming you're using the latest swagger-core, you should be able to create your own custom annotation with the annotations you want to repeat, and use that annotation across your API.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Puneet Jain

unread,
Dec 16, 2014, 2:26:13 PM12/16/14
to swagger-sw...@googlegroups.com
Thanks a lot for the help. Can you help with some pointer to the documentation for the same. I was not able to find the relative docs.

-Thanks,
Puneet
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Ron

unread,
Dec 16, 2014, 2:37:23 PM12/16/14
to swagger-sw...@googlegroups.com
I can't find a sample showing it, but this is a Java feature and is unrelated to Swagger directly.
You just need to create your own annotation and annotate the annotation itself with the Swagger annotations you want.

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

Mortier Andries

unread,
Mar 26, 2015, 8:09:45 AM3/26/15
to swagger-sw...@googlegroups.com
Hello Puneet,
 
i have the same issue, did you ever manage to solve this?
 
tx
 
 

Puneet Jain

unread,
Mar 26, 2015, 12:30:33 PM3/26/15
to swagger-sw...@googlegroups.com
Hi Mortier,

I solved it. The issue is because swagger Annotations are hardcoded in swagger play plugin. I cloned the swagger play plugin source code and customized it as per my need. I added few lines of code to reading an additional ApiResponse annotation at class level and added these responses in the responses of each and every method of that class.

Then compiled and published the plugin at my own artifactory.

Hope that will work for you.

-Puneet

--
You received this message because you are subscribed to a topic in the Google Groups "Swagger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swagger-swaggersocket/M80yb5tBnVw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.

Kumaresh Sitham

unread,
Apr 24, 2016, 12:36:26 PM4/24/16
to Swagger
Hello Puneet, I have same issue where I have repeated common Error map codes for almost all my classes. Will you able to share sample how do you achieve this ? It would be great help for me to proceed. Appreciate your help.
To unsubscribe from this group and all its topics, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

Puneet Jain

unread,
Apr 24, 2016, 9:08:21 PM4/24/16
to swagger-sw...@googlegroups.com
Hi Kumaresh,

I have downloaded the swagger source code and extended the functionality by my own and publish the library on our local artifactory. I don't the source code with me now, but it's not very complicated. You just need to understand how swagger code works, and add the hack at the right place.

Hope this will work.

-Puneet

To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "Swagger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swagger-swaggersocket/M80yb5tBnVw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.

Kumaresh Sitham

unread,
Apr 24, 2016, 9:18:16 PM4/24/16
to swagger-sw...@googlegroups.com

Hello Puneet, Thanks for quick reply !! I am new to Swagger. But, I somewhat understood what you were did on this. I will try to figure out.

Thanks again.

Regards,
《Kumar》
Reply all
Reply to author
Forward
0 new messages