Dynamically change RESTEasy service return type

不羁岁月 提交于 2019-12-07 10:14:54

问题


Am I able to change the value of @Produces annotation parameter in my RESTEasy services??
The task I'm given is to integrate multiple format reporting to an existing reporting system. So changing the @Produces annotation parameter dynamically would help me a lot.
Thanks in advance!


回答1:


Make your method return a Response object and try something like this;

int status = 200;
String type = MediaType.APPLICATION_XML;
String response = "<hello>world</hello>";
return Response.status(status).type(type).entity(response).build();

I think the type in the response will override what you annotated, but I haven't tested it.




回答2:


You can specify several entries in @Produces. Your request should mention which format (as mime type) do you want as the result.

Example:

@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })


来源:https://stackoverflow.com/questions/3786781/dynamically-change-resteasy-service-return-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!