java - Jersey Client - How to send List in a form with a POST request -


i using jersey client send post requests web server. have been building form object key-value pairs. however, have send list within request. here's abridged version of code

// phone pojo consisting of few strings public void request(list<phone> phones) {      form form = new form();      form.add("phones", phones);      clientresponse response = webservice.getresponsefromserver(form);      string output = response.getentity(string.class);      system.out.println(output); }  public static clientresponse getresponsefromserver(form form) {     client client = createclient();     webresource webresource = client.resource(path);      return webresource.type(mediatype.application_form_urlencoded).post(clientresponse.class, form); } 

unfortunately, doesn't seem work, , 400 bad request error. when send request directly

{"phones":[{"areacode":"217","countrycode":"01","number":"3812565"}]} 

i have no problems. in advance!

given example, based on typical pojo serialization, need isn't list<phone> class member phones of type list<phone>, otherwise payload this:

[{"areacode":"217","countrycode":"01","number":"3812565"}] 

so first, need jersey client json serialization feature. need include jersey-json (along jersey-client) in dependencies. example in maven:

<dependency>     <groupid>com.sun.jersey</groupid>     <artifactid>jersey-client</artifactid>     <version>1.19</version> </dependency> <dependency>     <groupid>com.sun.jersey</groupid>     <artifactid>jersey-json</artifactid>     <version>1.19</version> </dependency> 

create client this:

clientconfig clientconfig = new defaultclientconfig(); clientconfig.getfeatures().put(jsonconfiguration.feature_pojo_mapping, boolean.true); client client = client.create(clientconfig); 

assuming have variable phones pojo, can call this:

clientresponse response = webresource.accept(mediatype.application_json_type).type(mediatype.application_json_type).post(clientresponse.class, phones); 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -