0

I want to create a StudentApiController annotation for RequestMapping. It needs to add auto prefix for the RequestMapping.

Example usage 1:

@StudentApiController(value="/payment")
class PaymentEndpoint

mapping value needs to be "/student/payment"

Example usage 2:

@StudentApiController(value="/exam")
class ExamEndpoint

mapping value needs to be "/student/exam"

This is my code:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping
annotation class StudentApiController(
    val value: String = "",
    @get:AliasFor(annotation = RequestMapping::class, attribute = "value")
    val aliasValue: String = "/student" + value
)

I am getting this error: Default value of annotation parameter must be a compile-time constant

1 Answers1

0

Are you expecting all your endpoints to start with /student? If yes then you can use spring.mvc.servlet.path=/student or server.servlet.context-path=/student property in your application.properties.

If not then may be you can add logic in interceptor to prefix annotation value provided through your annotation. I haven't tried this for prefixing something in preHandle(), but I have tried it for postHandle() and it works.

Refer this link it appends thymeleaf's custom layout using custom annotation through interceptor. You can implement same logic for your requirement.

Edit 1

You may refer this answer which provides another approach/es to handle your requirement.

Vallabh Vaidya
  • 330
  • 2
  • 9