简体   繁体   中英

json deserialize using jackson api

pojo class

class Authentication {

 private static final long serialVersionUID = 3808270497038004505L;

    public Authentication(){
}

private String userName;

private String passWord;

public String getuserName() {
    return userName;
}

public void setuserName(String userName) {
    userName = userName;
}

public String getpassWord() {
    return passWord;
}

public void setpassWord(String passWord) {
    passWord = passWord;
}
}

controller

Authentication authentication= (Authentication) new JSONDeserializer<T>().deserialize(data,Authentication.class);
 return new ModelAndView().addObject(authentication);

passing data

  {"userName":"kadires","passWord":"ramgopal"}

result

System.out.println(authentication.getpassWord()+authentication.getuserName());

{"authentication":{"userName":null,"passWord":null}}

please help me

Check if your setters are correctly written. First letter after get must be uppercase.

Also you could create your implementation of NullSerializer. For example:

public class NullSerializer extends JsonSerializer<Object> {

    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        jgen.writeString("");
    }
}

Then just set your NullSerializer to the ObjectMapper:

ObjectMapper objMapper = new ObjectMapper();
StdSerializerProvider sp = new StdSerializerProvider();
sp.setNullValueSerializer(new NullSerializer());
objMapper.setSerializerProvider(sp);

问题可能是setter,它们以小写字母setuser ... setpassword开头,首字母大写,这是惯例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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