For an app working with:
- Spring Framework 4.3.3
- Jackson 2.8.2, modules used:
- jackson-annotations
- jackson-core
- jackson-databind
- jackson-dataformat-xml (it disables
JAXB 2
annotations)
I use jackson for JSON
and XML
.
About Spring for Rest working according with:
@PutMapping(value="/{id}",
consumes={MediaType.APPLICATION_XML_VALUE,
MediaType.APPLICATION_JSON_UTF8_VALUE})
and
@PostMapping(value="/{id}",
consumes={MediaType.APPLICATION_XML_VALUE,
MediaType.APPLICATION_JSON_UTF8_VALUE})
And for both scenarios exists the following parameter:
@Validated @RequestBody Persona persona,
The Persona
class is declared how:
@JacksonXmlRootElement(localName="persona")
public class Persona implements Serializable {
@JacksonXmlProperty(localName="id")
@JsonProperty("id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@JacksonXmlProperty(localName="nombre")
@JsonProperty("nombre")
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
...
Note validation annotations (@NotNull
, @Size
etc) have not been included above
Now about Testing for validation data purposes:
for an Entity
with:
null values such as:
person.setNombre(null)
person.setApellido(null)
empty values (of course, it only applies for String
type) such as:
person.setNombre("")
person.setApellido("")
When I use the invalid objects to be transformed in JSON and then is send to the rest method in JSON, I can confirm that when the data is transformed to Java again (Remember @Validated @RequestBody Persona persona
), through logging I can confirm:
Null scenario:
Invalid data is: Persona [id=null, nombre=null, apellido=null, ...]
Empty scenario:
Invalid data is: Persona [id=, nombre=, apellido=, ...]
for JSON I have
public static String objectToJson(Object object) throws JsonProcessingException {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = ow.writeValueAsString(object);
return json;
}
Until here all is ok.
The problem is with XML
Working with:
public static String objectToXml(Object object) throws JsonProcessingException {
XmlMapper xmlMapper = new XmlMapper();
return xmlMapper.writeValueAsString(object);
}
I did realize the following when the data invalid objects are transformed to XML to be sent to the rest controller when it arrives and is transformed again Java, the following happens:
Null scenario:
Invalid data is: Persona [id=null, nombre=null, apellido=null, ...]
Empty scenario:
Invalid data is: Persona [id=null, nombre=null, apellido=null, ...]
The problem is for the Empty scenario: the data arrives or is transformed how null. It should be
Invalid data is: Persona [id=, nombre=, apellido=, ...]
I need the data how an 'empty' value, not how a null value
Therefore how is possible fix this? I am assuming I need an extra configuration in some place.
BTW, Jackson for XML in Spring is:
@Bean
public MappingJackson2XmlHttpMessageConverter mappingJackson2XmlHttpMessageConverter(){
MappingJackson2XmlHttpMessageConverter converter = new MappingJackson2XmlHttpMessageConverter();
converter.setObjectMapper(jackson2ObjectMapperBuilder().createXmlMapper(true).build());
converter.setPrettyPrint(true);
converter.setDefaultCharset(StandardCharsets.UTF_8);
converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_XML));
return converter;
}
@Bean
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder(){
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.simpleDateFormat("yyyy-MM-dd");
return builder;
}
What I need:
Hello, What I need is the following:
If I send an Entity
with null
values for any field, it must arrives to the rest controller and the @RequestBody Persona
must contains the same the null
values for the same fields, same thought if the Entity
contains empty values and it is sent. It works how is expected in JSON
It only happens for XML
when the Entity
has null
values for any field. When the Entity
has empty values for any field. The @RequestBody Persona
contains null
values, it does not keep the empty
values, they have been transformed how null
instead. I need keep the same empty
values.
Therefore if the Entity
has been sent with either null
or empty
values or both, I need that JSON
and XML
when the @RequestBody Persona
works, it contains the same null
or empty
values for the same fields how they have been defined originally.
Note seems it is a bug, closely related with: