JAX-RS by default @Produces

Is there a default way to use @Produces annotation on all JAX-RS / resources?

I have many classes that produce web services. Instead of betting @Produces({"application/json", "application/xml"})on each of them, I would like to do it in a central place. Thus, I can add future producers in one place, and not change each class.

I am currently using Resteasy with Jetty.

+4
source share
2 answers

I know this is a bit of an old question, but it might be useful for someone else like me. In addition to the ccleve solution, you can also use interfaces. Due to multiple inheritance, you can combine several types of media through interfaces. Example:

@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface IJsonResource {
}

and

@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_XML)
public interface IXmlResource {
}

Then in your specific JAX-RS resource class:

public class SomeJaxRsResource implements IJsonResource, IXmlResource {
...
}
+1
source

I have found the answer. Create a parent / resource class, add the @Produces annotation to it, and then subclass it for real resources. It works great.

0
source

All Articles