json - Why does response.readEntity(Post.class) return null? -
i'm trying json data rest resource , automatically convert java object json-to-java binding. use jersey framework 2.21 jersey-media-moxy module json provider in client application .
i cannot figure out why null instead of proper post object when this:
client client = clientbuilder.newclient(); webtarget webtarget = client.target("http://www.travelportland.com/wp-json"); response response = webtarget.path("posts/9").request().get(); post post = response.readentity(post.class); // => null
the post class implementation looks (at point want take 'title' field json):
@xmlrootelement public class post { private string title; public string gettitle() { return title; } public void settitle(string title) { this.title = title; } }
all works fine when try string:
string poststr = response.readentity(string.class);
or if try other resource:
webtarget webtarget = client.target("http://jsonplaceholder.typicode.com"); response response = webtarget.path("posts/9").request().get(); post post = response.readentity(post.class); // => com.example.post@74e28667
it seems issue somehow related structure or size of json data. how can solve problem?
seems issue moxy. not sure (maybe size, bad chars, don't know). tested jackson, , works fine. might want make switch jackson if can't figure out moxy.
<dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-json-jackson</artifactid> <version>${jersey2.version}</version> </dependency>
you need set class annotation ignore unknown properties if not model keys json
@xmlrootelement @jsonignoreproperties(ignoreunknown = true) public class post {
jackson recognize jaxb annotations if using bunch of them.
also make sure rid of the moxy dependency of have explicitly register jacksonfeature.class
client
, disable moxy provider.
Comments
Post a Comment