Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

restful webservice with xml or json as parameter

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.

like image 333
wib Avatar asked Jul 02 '15 10:07

wib


People also ask

Does the RESTful method support JSON or XML or both format?

The same resource may return either XML or JSON depending upon the request, but it shouldn't return both at the same time.

Why JSON and XML are used in RESTful web services?

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.

Is REST JSON or XML?

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).

Does RESTful API support XML?

Data types that REST API can return are as follows: JSON (JavaScript Object Notation) XML. HTML.


1 Answers

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

like image 141
bobK Avatar answered Sep 21 '22 14:09

bobK