Jackson [JSON to Java]: attribut du json différent de l'attribut du bean
Bonjour,
Soit un JAVA BEAN et un JSON
JSON
Code:
{"attributJson":"test"}
JavaBean
Code:
1 2 3 4 5 6 7 8 9 10 11
| public class MonBean {
public String attribut;
public String getAttribut() {
return attribut;
}
public void setAttribut(String attribut) {
this.attribut = attribut;
}
} |
Comment dois-je effectuer la désérialisation via la librairie Jackson sachant que je ne veux pas :
- utiliser les annotations
- créer une autre classe que MonBean
A mon avis ,je dois définir une classe qui hérite de PropertyNamingStrategy mais je ne sais pas ce qu'il faut faire.Avez-vous une idée?
Code:
1 2 3 4
| String monJson = "{\"attributJson\":\"test\"}";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(new MaPropertyNamingStrategy());
MonBean monBean = objectMapper.readValue(monJson,MonBean .class); |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
private class MaPropertyNamingStrategy extends PropertyNamingStrategy {
@Override
public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
return defaultName;
}
@Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return defaultName;
}
@Override
public String nameForGetterMethod(MapperConfig<?> config,AnnotatedMethod method, String defaultName){
return defaultName;
}
} |