I have a simple question the answer to which I have been trying to find out over the restricted internet connection in my office but to no avail.
1) How to create a restful web service in java preferably using netbeans that accepts xml and/or json as the parameter and how do I process it.
2) How do I call these web services. I mean how can we pass xml in the url? Or is there any other way?
I would prefer using jersey if I have to use APIs. I am sorry if the question is too generic, but I need all the knowledge I can get on this in relatively short time.
The same resource may return either XML or JSON depending upon the request, but it shouldn't return both at the same time.
In REST architecture, a REST Server simply provides access to resources and REST client accesses and modifies the resources. Here each resource is identified by URIs/ global IDs. REST uses various representation to represent a resource like text, JSON, XML. JSON is the most popular one.
Unlike SOAP, REST doesn't have to use XML to provide the response. You can find REST-based web services that output the data in Command Separated Value (CSV), JavaScript Object Notation (JSON) and Really Simple Syndication (RSS).
Data types that REST API can return are as follows: JSON (JavaScript Object Notation) XML. HTML.
You can do this. I currently am working on webservices that do this.
Use these annotations:
@POST
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Object create(Object object);
On the objects you want to pass, you can annotation from the javax.xml.bind.annotation package. This way, java can marshal/unmarshal these itself.
@XmlRootElement(name = "Something")
@XmlAccessorType(XmlAccessType.NONE)
public class A {
private static final long serialVersionUID = 6478918140990163091L;
@XmlElementWrapper(name = "collectionWrapper")
@XmlElement(name = "collectionItem")
private final Collection<Object> domainCollection = new LinkedList<Object>();
}
To access it do something like this:
final Builder request = ClientBuilder.newClient().target(getBaseUri()).path("url").request(MediaType.APPLICATION_XML);
return request.post(Entity.entity(param, MediaType.APPLICATION_XML)).readEntity(A.class);
Follow this tutorial for examples: http://www.vogella.com/tutorials/REST/article.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With