java - Why annotation @JsonValue does not work in Jersey? -


i have restful service based on jersey framework.

i need serialize , deserialize object wich include enum fields.

i wrote 2 methods , annotated @jsonvalue , @jsoncreator. jersey doesn't show enum fields?

my object enum:

@xmlrootelement public class recipientwrapper{     @apimodelproperty(hidden = true)     private recipient recipient;      private string name;     private string address;     private recipienttype type;      public recipientwrapper(){}      public recipientwrapper(string name, string address, message.recipienttype type) {         recipient = new recipient(name, address, type);     }      @xmltransient     public recipient getrecipient(){         if (recipient == null && name != null && address != null){             if (type != null){                 recipient = new recipient(name, address, type.tomsgrecipienttypevalue());             }             else{                 recipient = new recipient(name, address, message.recipienttype.to);             }         }         return recipient;     }      public void setrecipient(recipient recipient){          this.recipient = recipient;     }      public string getname(){         return recipient.getname();     }      public string getaddress(){         return recipient.getaddress();     }      public message.recipienttype gettype(){         return recipient.gettype();     }      public void setname(string name) {         this.name = name;     }      public void setaddress(string address) {         this.address = address;     }      public void settype(recipienttype type) {         this.type = type;     }      public enum recipienttype {         to,         cc,         bcc;          private static map<string, recipienttype> namemap = new hashmap<string, recipienttype>();         private static map<recipienttype, message.recipienttype> msgrecipienttype = new hashmap<recipienttype, message.recipienttype>();          static{             namemap.put("to", to);             namemap.put("cc", cc);             namemap.put("bcc", bcc);              msgrecipienttype.put(to, message.recipienttype.to);             msgrecipienttype.put(cc, message.recipienttype.cc);             msgrecipienttype.put(bcc, message.recipienttype.bcc);         }          @jsoncreator         public static recipienttype forvalue(string value){             return namemap.get(stringutils.uppercase(value));         }          @jsonvalue         public string tovalue(){             (map.entry<string, recipienttype> entry: namemap.entryset()){                 if (entry.getvalue() == this){                     return entry.getkey();                 }             }             return null;         }          public message.recipienttype tomsgrecipienttypevalue(){             (map.entry<recipienttype, message.recipienttype> entry: msgrecipienttype.entryset()){                 if (entry.getkey() == this){                     return entry.getvalue();                 }             }             return null;         }     } } 

and gradle dependencies:

compile 'javax.ws.rs:jsr311-api:1.1.1'  compile 'com.sun.jersey:jersey-server:1.18.1' compile 'com.sun.jersey:jersey-servlet:1.18.1' compile 'com.sun.jersey:jersey-bundle:1.18.1' compile 'io.swagger:swagger-jersey-jaxrs:1.5.0'  compile 'org.antlr:stringtemplate:4.0.2' compile 'org.freemarker:freemarker:2.3.22' compile 'org.apache.velocity:velocity:1.7'  compile 'org.codemonkey.simplejavamail:simple-java-mail:2.2'  compile 'org.slf4j:slf4j-simple:1.7.12' compile 'org.slf4j:slf4j-api:1.7.12'  compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13' 

json output:

{   "address": "mymail@gmail.com",   "name": "my name" } 

why jersey doesn't show enum fields?

the problem @jsonvalue annotation. if remove - work.

example:

public class {     private string type;     private recipienttype recipienttype;      public a(string type, recipienttype recipienttype) {         this.type = type;         this.recipienttype = recipienttype;     }      public string gettype() {         return type;     }      public recipienttype getrecipienttype() {         return recipienttype;     }      public enum recipienttype {         to,         cc,         bcc;     } } 

when serializing get

{"type":"a","recipienttype":"bcc"}

jackson knows handle enums, don't need special it. add @jsonvalue ruined it.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -