json使用jackson api反序列化

时间:2013-12-24 14:12:52

标签: java json rest spring-mvc

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;
}
}

控制器

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

传递数据

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

结果

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

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

请帮帮我

2 个答案:

答案 0 :(得分:0)

检查您的二传手是否写得正确。 get之后的第一个字母必须是大写的。

您还可以创建NullSerializer的实现。例如:

public class NullSerializer extends JsonSerializer<Object> {

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

然后将NullSerializer设置为ObjectMapper:

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

答案 1 :(得分:0)

问题可能是设置者,他们从小写setuser ... setpassword开始,使用大写字母作为第一个字母,这是惯例。