end-user end-user - 4 months ago 38
Java Question

How can I use a constant to enter multiple @CrossOrigins?

I have a

annotation on several of my controllers. I'd like to be able to set a
static final
somewhere and have all of them refer to it. Something like:

public static final String[] ORIGINS = {"domain1","domain2"};
@CrossOrigin(origins = ORIGINS)

However, I clearly have some syntax off.

For those who are interested in doing the same thing, here's what I did:

@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE})
@CrossOrigin(origins = {
@interface CORSPermittedController {

That allows me to annotate all my controllers with


Unfortunately, you can't do that with Java syntax. Attribute values must be compile constants (primitive, String literals, or arrays of these types).

But you could write a bean post processor which is responsible for putting this annotation over a class at runtime by using javassist.