Ignorar un campo específico en la serialización con Jackson
Estoy usando la biblioteca Jackson.
Quiero ignorar un campo específico al serializar / deserializar, por ejemplo:
public static class Foo {
public String foo = "a";
public String bar = "b";
@JsonIgnore
public String foobar = "c";
}
Debería darme:
{
foo: "a",
bar: "b",
}
Pero estoy recibiendo:
{
foo: "a",
bar: "b",
foobar: "c"
}
Estoy serializando el objeto con este código:
ObjectMapper mapper = new ObjectMapper();
String out = mapper.writeValueAsString(new Foo());
El tipo real del campo en mi clase es una instancia de la clase LOG4J Logger. ¿Qué estoy haciendo mal?
56
Author: Edison Gustavo Muenz, 2012-01-04
3 answers
Ok, así que por alguna razón me perdí esta respuesta .
El siguiente código funciona como se espera:
@JsonIgnoreProperties({"foobar"})
public static class Foo {
public String foo = "a";
public String bar = "b";
public String foobar = "c";
}
//Test code
ObjectMapper mapper = new ObjectMapper();
Foo foo = new Foo();
foo.foobar = "foobar";
foo.foo = "Foo";
String out = mapper.writeValueAsString(foo);
Foo f = mapper.readValue(out, Foo.class);
85
Author: Edison Gustavo Muenz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:03:02
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:03:02
También vale la pena destacar esta solución usando DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES: https://stackoverflow.com/a/18850479/1256179
1
Author: rwblackburn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 10:31:16
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 10:31:16
Referencia de ¿Cómo puedo decirle a Jackson que ignore una propiedad para la que no tengo control sobre el código fuente?
Puedes usar Jackson Mixins. Por ejemplo:
class YourClass {
public int ignoreThis() { return 0; }
}
Con este Mixin
abstract class MixIn {
@JsonIgnore abstract int ignoreThis(); // we don't need it!
}
Con esto:
objectMapper.addMixIn(YourClass.class, MixIn.class);
0
Author: Tong,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-08 19:00:26
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-08 19:00:26