java - JAX-RS JAXB Jackson not using @XmlRootElement name -


i developing restful application jax-rs , jaxb. want send following entity json client:

@xmlrootelement(name = "user") @xmlaccessortype(xmlaccesstype.field) public class userdto implements serializable {     private static final long serialversionuid = 1l;      private long id;     private string username;     private string firstname;     private string lastname;      // getter & setter } 

the method in webservice defined follows:

@post @path("users/{id}") @produces({ mediatype.application_json, mediatype.application_xml }) public useraccountdto login(@pathparam("id") long id) {     useraccountdto useraccount = loaduseraccount(id);     return useraccount; } 

first problem was, root node not send via json. therefore have added following class:

@provider @produces(mediatype.application_json) public class skedflexcontextresolver implements contextresolver<objectmapper> {     private objectmapper objectmapper;      public skedflexcontextresolver() throws exception     {         this.objectmapper = new objectmapper().configure(serializationfeature.wrap_root_value, true);     }      @override     public objectmapper getcontext(class<?> objecttype)     {         return objectmapper;     } } 

now, root node send data. in case of xml fine (root node equal name of @xmlrootelement). see following xml response:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <user>   <id>10</id>   <username>admin</username>   <firstname>administrator</firstname> </user> 

but in case of json root node classname of pojo:

{     "useraccountdto":     {         "id": 10,         "username": "admin",         "firstname": "administrator",         "lastname": null     } } 

why differs output between xml , json? need change specified name in @xmlrootelement-annotation

changing .configure(serializationfeature.wrap_root_value, true) .configure(serializationfeature.wrap_root_value, false) should help.

according javadoc:

feature can enabled make root value <..> wrapped within single property json object, key "root name"


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -