How to Consume JSON as input in PUT and POST method of REST webservice in java

前提是你 提交于 2019-12-07 17:33:49

问题


I am trying to create a REST web service using JAX-RS. In that, I have PUT method or POST method which consumes the json as mediatype in the REST web service. Can I know how to call these methods from the client side. How do we pass that json as input from client side to those PUT and POST method and how would we consume the json format in the PUT or POST method from server side. If we want to consume xml, then we are using JAXBElement. For consuming json, how to do that ?


回答1:


This may help get you going: http://blog.sertik.net/labels/jersey.html

From my (extremely rusty) recollection, you sort of treat the @PUT methods the same way you treat @POST methods. So as shown in that blog entry, try using the @FormParam annotations. Also, read over the Jersey API to see if anything looks useful.

The main difference between them (PUT/POST) is in the meaning; PUT typically creates a new resource at the uri, whereas POST can 'append to' it (there are also a few other meanings to what exactly POST does).

PS almost forgot to mention, cURL is so.... nice.




回答2:


Hey there is a built in support for JSON in JAX-RS.For this you just need to write the POJO class with JAXB annotations. JAX-RS has built in MessageBodyReaders and MessageBodyWriters to support.If you want to POST i.e., sending the Custom Data you need to write your own MessageBodyReaders/Writers and register them with the Client.



来源:https://stackoverflow.com/questions/3861387/how-to-consume-json-as-input-in-put-and-post-method-of-rest-webservice-in-java

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