java - Jersey/JAX-RS clients that use Jackson to pass POJOs as entities -


i trying implement restful web service client using jersey/jax-rs:

public class myclient implements closeable {     private client client;     private fizzresource fizzresource;     // several other resources omitted brevity.      // ctor, getters , setters, etc.      @override     public void close() throws exception {         client.destroy();         client.getexecutorservice().shutdown();     } }  public class fizzresource {     private client client;      public fizz savefizz(fizz fizz) {         webresource webresource = client.resource("whatever");         clientresponse response = webresource.accept(???).post(???);         if(response.getstatus() != 200) {             // something...         } else {             // else...         }     } } 

my problem not want work json; instead want work directly entities (e.g. fizz). use jackson automagically serialization between json , entities (without me having explicitly conversion inside each method), i'm not seeing how possible/doable. ideally savefizz method might like:

public fizz savefizz(fizz fizz) {     webresource webresource = client.resource("whatever");      clientresponse response = webresource.accept("application/json").post(fizz);     if(response.getstatus() != 200) {         throw new runtimeexception("errors bad, mkay?");     }      fizz fizz = response.extractsomehow();      return fizz; } 

assume fizz class annotated correct jackson annotations (jsonproperty, etc.).

any ideas?

you're using jersey 1.x, have @ the user guide json/pojo support

first thing: need make sure have jersey-json module

<dependency>     <groupid>com.sun.jersey</groupid>     <artifactid>jersey-json</artifactid>     <version>${jersey-version}</version> </dependency> 

this module have needed messagebodyreader , messagebodywriter read , write pojos , json

second thing: need make sure enable pojo mapping support feature. both server/application , client

server web.xml

<init-param>     <param-name>com.sun.jersey.api.json.pojomappingfeature</param-name>     <param-value>true</param-value> </init-param> 

server programmatic

public class myapplication extends packagesresourceconfig {     public myapplication() {         getfeatures()..put(jsonconfiguration.feature_pojo_mapping, boolean.true);     } } 

see other deployment options

client config

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

third thing: need make sure our resource method annotated , make client call (to allow correct writers/readers discovered).

for methods accepting json, should annotated @consumed("application/json") , if method produces response in json, should annotated @produces("application/json"). depends on semantics of method, annotations include, 1 or both.

for client, long have correct configuration, extracting java object, matter of calling getxxx java type.

public void testgetfizz() {     // directly extact     fizz fizz = r.path("fizz").accept("application/json").get(fizz.class);     system.out.println(fizz);      // extract clientresponse     clientresponse response = r.path("fizz").                      accept("application/json").get(clientresponse.class);     fizz fizz1 = response.getentity(fizz.class);     system.out.println(fizz1); } 

here other pieces of code used test

@path("/fizz") public class fizzresource {      @post     @consumes("application/json")     public response postfizz(fizz fizz) {         system.out.println("==== created fizz ===");         system.out.println(fizz);         system.out.println("=====================");         return response.created(null).build();     }      @get     @produces("application/json")     public response getfizz() {         fizz fizz = new fizz(1, "fizz");         return response.ok(fizz).build();     } } 

server config

resourceconfig resourceconfig = new packagesresourceconfig("test.json.pojo"); resourceconfig.getfeatures().put(                        jsonconfiguration.feature_pojo_mapping, boolean.true); 

client config

clientconfig clientconfig = new defaultclientconfig(); clientconfig.getfeatures().put(jsonconfiguration.feature_pojo_mapping,                                 boolean.true); client client = client.create(clientconfig); r = client.resource(main.base_uri); // r = webresource 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -