6

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.

3
  • 1
    What if any provided suggested link to learn this is restricted in your office? ;)
    – Garry
    Jul 2, 2015 at 10:51
  • I am expecting that..but worth a try
    – wib
    Jul 2, 2015 at 10:53
  • 1
    Both of your questions are very broad. It makes it seems like you have zero experience. If that's the case I suggest running through a tutorial. Personally I would just start at the top in the user guide. The first chapter will get you up and running. Third chapter will go more in depth with the basics. And chapter nine will get you on your way to handling JSON and XML. Another way to get started in Netbeans (real quick so you can skip chapter 1) is to see the bottom of this answer Jul 2, 2015 at 11:03

3 Answers 3

3

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

2

These are the general steps on how to do this (i assume you already have installed java and the corresponding environment variables):

1) Download and install Apache Tomcat. Configure Netbeans to identify the Apache Tomcat instance you have extracted/installed.

2) Download jersey jar files and add them to your Web Project from here (link: Jersey JAX-RS 2.0 RI bundle), or use the required dependencies if you are working with maven. Don't forget to add the project to the Apache Tomcat server.

3) Create a Jersey-based java class inside the source folder of your project. In each restful function you will define what data you will accept, how you will proccess them and what you will send. Here is a very basic example:

@Path("/server")
public class RestServer {

   @POST
   @Consumes(MediaType.TEXT_XML)
   @Produces(MediaType.TEXT_XML)
   public String basicPOSTRequest_XMLResponse(String xmlString) {           
      System.out.println("Received: " + xmlString);
      return doSomethingWithString(xmlString);
   }
}

4) Create an 'index.html' file in the webContent folder containing the corresponding ajax calls for your restful functions. (the ones you have created in the jersey class). In each ajax call, you will send and receive your data using jQuery functionality. Here is a basic ajax call example:

function ajaxCall(xmlData) {

$.ajax({
    type: "POST",
    url: _baseURI + "/server",
    contentType: "text/xml",
    data: xmlData,  
    datatype: "text/xml",
    success:
            function (data, textStatus, jqXHR){
                alert(data);
            },
    error: 
            function (jqXHR, textStatus, errorThrown) {
                alert("error");
            }
});
}

Note that ajax is just one way to use your restful functions. For example you could do it with another java (or any other language) program that can send Http calls.

5) Start the tomcat server from eclipse.

6) Use your index.html file by hitting it's url (usually is something like: 'http://localhost:8080/-yourProjectName-') to check the restful functionality of your project.

The above are just guidelines. If you want more details in any step, tell it to me in order to edit my answer.

1

You can take reference to this link. And I hope that this link can be accessed from within your office.

Furthermore, accepting type of the parameter is a base for your requirement. There are two annotations used for the accepting and responding type which are respectively @consume(MediaType) and @produce(MediaType). You will also have to specify the MediaType, like

MediaType.APPLICATION_JSON

or

MediaType.APPLICATION_XML_TYPE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.