简体   繁体   English

如何使用Enum将Json反序列化为Java Object?

[英]How to deserialize Json to Java Object with Enum?

I have the following json i want to deserialize to a Java Object : 我想将以下json反序列化为Java对象:

{
"firstname": "John",
"lastname": "Do",
"sex": "",

}

In the other part i have a Java Rest Webservice : 在另一部分中,我有一个Java Rest Web服务:

@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response findPerson(@Valid PersonneRequest personne) {

    if(personne!= null){
       Personne result = null;
      // do some stuff 

        return Response.ok(result).build();
    }

    // Bad request
    return Response.status(Response.Status.BAD_REQUEST).build();


}

Here is the Personne class : 这是Personne类别:

public class PersonneRequest implements Serializable{ 公共类PersonneRequest实现了Serializable {

private String firstname;

private String lastname;

private Sex sex;

// getters and setters //获取器和设置器

} }

Here is my Sexe class : 这是我的Sexe类:

public enum Sex{


    MAN("man"),
    WOMAN("woman");

    private String type;

    private Sex(String type){
        this.type = type;
    }

    public String getType(){
        if(type!=null && !StringUtils.isEmpty(type)){
            return type;
        }
        return MAN.getType();
    }
}

My problem is when i make a post call on my webervice. 我的问题是我在网络服务台上发帖时。 Json data are not deserialize when sex is empty. 当性别为空时,Json数据不会反序列化。 I am getting the following exception. 我收到以下异常。 How to set a value for empty value of enum ? 如何为枚举的空值设置值? Here is my java exception : 这是我的Java异常:

org.codehaus.jackson.map.JsonMappingException: Can not construct instance of Sex from String value '': value not one of declared Enum instance names
 at [Source: org.glassfish.jersey.message.internal.EntityInputStream@5ed82ec0; org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
    at org.codehaus.jackson.map.deser.StdDeserializationContext.weirdStringException(StdDeserializationContext.java:243) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
    at org.codehaus.jackson.map.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:74) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
    at org.codehaus.jackson.map.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:23) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
    at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
    at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
    at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2695) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1308) ~[jackson-mapper-asl-1.9.2.jar:1.9.2]
    at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419) ~[jackson-jaxrs-1.9.13.jar:1.9.13]

Thanks 谢谢

Use @JsonInclude(JsonInclude.Include.NON_EMPTY) with property Sex . @JsonInclude(JsonInclude.Include.NON_EMPTY)与属性Sex一起使用 It will handle empty string value. 它将处理空字符串值。

@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Sex sex;

I hope this may help you. 希望对您有所帮助。

I thinks that there are two problem in your code: Firstly, your getType() method in class enum Sex is wrong. 我认为您的代码中存在两个问题:首先,类枚举Sex中的getType()方法是错误的。 It is return MAN.getType() that mean call itself so that cause infinitive loop. 返回MAN.getType()意味着调用自身,从而导致无限循环。 May be you want to catch the case that null is assigned for enum but its not the right way. 可能是您想为枚举分配null而不是正确的方法。

Secondly, the reason that directly related to your problem in log file is can not assign null or empty value for enum type. 其次,与日志文件中的问题直接相关的原因是无法为枚举类型分配null或空值。 I think use @JsonIgnoreProperties and DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL is the best way to solve your problem Refer the post here: How to ignore enum fields in Jackson JSON-to-Object mapping? 我认为使用@JsonIgnoreProperties和DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL是解决问题的最佳方法。请参阅此处的文章: 如何忽略Jackson JSON到对象映射中的枚举字段?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM