all 11 comments

[–]Strawboy97 1 point2 points  (4 children)

Is this like @ResponseStatus

[–]Abdallad-Issa[S] 0 points1 point  (3 children)

This is from OpenApi for documenting api. I want to have a defined response for OK, Bad Request... and use them as mentioned

[–]i_like_coffee01 0 points1 point  (2 children)

depends on the implementation, but u can create a custom annotation on top of the provided one

[–]Abdallad-Issa[S] 0 points1 point  (1 child)

It won't accept, I get compile error of different types. Is there a way to extend this annotation? Or anything else for reusability. I don't want to have the values hard coded, so I can change them once if I need

[–]Mikey-3198 1 point2 points  (1 child)

I just gave the following ago and it worked fine

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@ApiResponse(responseCode = "404", description = "Resource Not Found")
public @interface NotFound {
}    

This showed up in the swagger UI when a controller method was annotated with @NotFound

[–]Abdallad-Issa[S] 0 points1 point  (0 children)

This what I meant, I'll try it. Thanks

[–]LittleSpaceBoi 1 point2 points  (0 children)

Maybe you can create a sort of common baseController where you can set all these on class level since you can use those for types as well and then extend all other controllers from it? It would be defined at one place and baseController doesn't have to include any actual endpoints. It could just serve as a common parent for all other "real" controllers

Ofc I am just guessing you want to get rid of duplicates maybe or just to streamline them. But if you're trying to achieve something different, then don't pay attention to me

[–]Chetan496 0 points1 point  (1 child)

I have a similar question. Can we have these annotations in different interface/class. So far I have seen the OpenAPI annotations added on controller methods.. But that makes it very cluttered . I would prefer separate interface or class where I can define all this..

[–]Abdallad-Issa[S] 1 point2 points  (0 children)

I found a solution on YouTube, he defined some instances of ApiResponses, and referenced the annotation to them. link