java - Need to send arraylist to webservice from client and return modified arraylist -


i have started out creating basic webservice @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 run it. have jar files , web.xml file setup etc. want next create client sends arraylist of strings webservice , returns modified version of arraylist client. question first of how set client? , how run sends arraylist server. ive read lot of tutorials iam finding of them hard follow. im new making webservices in java. thank in advance

first of all, sorry copied lot of code project working on currently, cumbersome otherwise. please excuse if have typos or compile errors. also, beware need external library 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 array list");     }      public static void main(string ... args) {            client c= new client();            myarraylist result= c.sendarraylist(new myarraylist(/*whatevs goes inside*/));     }  } 

in server need like:

    @post     @path("/sendarraylist")     @consumes(mediatype.application_xml)     @produces(mediatype.application_xml)     myarraylist modifylist(@queryparam("arraylist") myarraylist list) {         //do stuff     } 

the last thing left create myarraylist class according jaxb rules (see example: http://www.vogella.com/articles/jaxb/article.html)


Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -