Reputation: 288
I need to know if there is a way by which I can set the @Produces value from query parameter. For example: if the query parameter value is 'JSON' then I want to set the method header as @Produces(application/json) or if it is 'XML' then I want to set it as @Produces(application/xml)
I know that we can specify multiple media types as @Produces({"application/json", "application/xml"}), but it is not working for me as it is giving me an error when the output is JSON.
Many thanks!
Upvotes: 2
Views: 5185
Reputation: 279920
The Java Language Specification has rules for what can go into an annotation's attributes. Specifically, it says this
It is a compile-time error if the return type of a method declared in an annotation type is not one of the following: a primitive type, String, Class, any parameterized invocation of Class, an enum type (§8.9), an annotation type, or an array type (§10) whose element type is one of the preceding types.
Basically, annotations (and their attributes' values) are meant to be compile time constants. So, no, you cannot change the value of the annotation itself.
What you can do is follow this and make your handler method return a Response
object with the appropriate media type.
Upvotes: 2