我有基于Jersey框架的RESTful服务。
我需要序列化和反序列化一些包含枚举字段的对象。
我写了两个方法并用@JsonValue
和@JsonCreator
注释了它们。但泽西岛没有显示枚举字段?
我的枚举对象:
@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(){
for (Map.Entry<String, RecipientType> entry: nameMap.entrySet()){
if (entry.getValue() == this){
return entry.getKey();
}
}
return null;
}
public Message.RecipientType toMsgRecipientTypeValue(){
for (Map.Entry<RecipientType, Message.RecipientType> entry: msgRecipientType.entrySet()){
if (entry.getKey() == this){
return entry.getValue();
}
}
return null;
}
}
}
gradle依赖项:
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输出:
{
"address": "mymail@gmail.com",
"name": "My name"
}
为什么泽西岛不显示枚举字段?
答案 0 :(得分:0)
问题是@JsonValue
注释。如果你删除它 - 它会工作。
示例:
public class A {
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;
}
}
序列化时我得到了
{"type":"a","recipientType":"BCC"}
杰克逊本身知道处理枚举,你不需要任何特别的东西。添加@JsonValue
只会破坏它。