问题
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