0

I have started out by creating a basic webservice with a @GET method shown below.

package com.webservice.eoin;

import java.awt.PageAttributes.MediaType;
import java.util.ArrayList;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;



@Path("/webservice")

public class Web_service {
@PUT
@Path("/getname")
@Produces("text/plain")

  public String getname()
  {
return "hello!!!";
  }




} 

this works fine when I run it. I have all the jar files and web.xml file setup etc. What i want to do next is create a client that sends an arraylist of strings to the webservice and returns a modified version of the arraylist to the client. My question is first of all how do you set up the client? and how do I run it so it sends this arraylist to the server. Ive read a lot of tutorials but Iam finding some of them hard to follow. Im new to making webservices in Java. Thank you in advance

1 Answer 1

2

First of all, I am sorry I copied a lot of code from the project I am working on currently, but it would be cumbersome otherwise. Please excuse if I have typos or compile errors. Also, beware that you need an external library for this.

The client:

import java.net.URI;
import java.util.List;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.client.apache.ApacheHttpClient;
import com.sun.jersey.client.apache.config.ApacheHttpClientConfig;
import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;

public Client {

    private final URI fServerURI;
    private final ApacheHttpClient fClient;
    private final MediaType fMediaType= MediaType.APPLICATION_XML_TYPE;

    public Client() {
            final String apiEndpoint= "...";
            final DefaultApacheHttpClientConfig clientConfig;
            fServerURI= UriBuilder.fromUri(apiEndpoint).build();
            clientConfig= new DefaultApacheHttpClientConfig();
            clientConfig.getProperties().put(ApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES, true);
            fClient= ApacheHttpClient.create(clientConfig);
    }

    private <T> T call(WebResource resource, RequestType requestType, Object requestEntity, GenericType<T> acceptType, String taskMessage) {
            return acceptCall(resource, requestType, acceptType, requestEntity);
    }

    private <T> T acceptCall(WebResource resource, RequestType requestType, GenericType<T> acceptType, Object requestEntity) {
           switch (requestType) {
           case POST:
                return resource.accept(fMediaType).post(acceptType, requestEntity);
           case PUT:
                return resource.accept(fMediaType).put(acceptType, requestEntity);
           case DELETE:
                resource.accept(fMediaType).delete();
                return null;
           default:
                return resource.accept(fMediaType).get(acceptType);
    }

    public MyArrayList sendArrayList(MyArrayList list) {
           WebResource resource= createResource();
           resource= resource.path("webservice").path("sendarraylist");
           resource= resource.queryParam("arraylist", list);
           return call(resource, RequestType.POST, null, new GenericType<MyArrayList>() {
    }, "Send my array list");
    }

    public static void main(String ... args) {
           Client c= new Client();
           MyArrayList result= c.sendArrayList(new MyArrayList(/*whatevs goes inside*/));
    }
 }

In the server you need something like:

    @POST
    @Path("/sendarraylist")
    @Consumes(MediaType.APPLICATION_XML)
    @Produces(MediaType.APPLICATION_XML)
    MyArrayList modifyList(@QueryParam("arraylist") MyArrayList list) {
        //do stuff
    }

The last thing that is left is to create MyArrayList class according JAXB rules (see this example: http://www.vogella.com/articles/JAXB/article.html)

Sign up to request clarification or add additional context in comments.

1 Comment

Also I was just wondering should I run a client in a seperate application?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.