Using @Produces for different methods [closed]

萝らか妹 提交于 2019-12-06 13:43:24

I didn't know you can have 2 "@GET" for the same @Path. I guess one could assume jersey will route it to the right method based of "accepts" header.

I understand this is a test, but I don't know how useful is a text representation of an object.

Here's how and when I use @Produces

Typically, my web services will either serve XML or JSON. Any typical services would produces both, according to the client preference (using the accept header), as per your doGetAsXmlOrJson method

You don't need to produce the XML or the JSON yourself, just return a POJO, and Jersey will use Jackson or Moxy (or your favorite JSON / XML provider as configured with Jersey) to convert it for you, outputing JSON or XML based of your "accepts" header.

Sometimes, some services may return images, pdf, or even text :) ... and in these specials cases, I use the other possible values of @Produces...

But I never add a case where I wanted automatic JSON/XML marshalling or text.. I guess it could be useful for debugging, but JSON and XML are usually human readable.

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