Reputation: 55
I am having a question with Jackson that I think should be simple to solve, but it is killing me.
I have a java POJO class that looks like this (with getters and setters)
@JsonRootName(value = "notificacion")
public class NotificacionDTO extends AbstractDTO {
@JsonProperty(required=true)
private Integer instalacion;
@JsonProperty(required=true)
private Integer tipoNotificacion;
@JsonProperty(required=true)
private String mensaje;
}
Here is the AbstractDTO
public abstract class AbstractDTO implements Serializable {
public void validate() {
Field[] declaredFields = this.getClass().getDeclaredFields();
for (Field field : declaredFields) {
if (field.isAnnotationPresent(JsonProperty.class)){
if (field.getAnnotation(JsonProperty.class).required() && this.isNullParameter(field)) {
throw new RuntimeException(String.format("El parametro %s es null o no esta presente en el JSON.", field.getName()));
}
}
}
}
private boolean isNullParameter(Field field) {
try {
field.setAccessible(true);
Object value = field.get(this);
if (value == null) {
return true;
} else if (field.getType().isAssignableFrom(String.class)) {
return ((String) value).isEmpty();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
}
I want to deserialize JSON that looks like this into a NotificacionDTO object:
{
"notificacion":
{
"instalacion":"1",
"tipoNotificacion":"2",
"mensaje":"Un Mensaje"
}
}
This is my EndPoint
@Controller
@RequestMapping("/notificacion")
public class NotificacionEndPoint extends AbstractEndPoint{
@Autowired
private NotificacionService service;
@RequestMapping(value = {"", "/"}, method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void addNotification(@RequestBody NotificacionDTO notification) throws JsonParseException, JsonMappingException, IOException {
this.log.info("[POST RECEIVED] = " + notification);
notification.validate();
this.service.addNotification(notification);
}
}
I hava a custom ObjectMapper with this
public class JsonObjectMapper extends ObjectMapper {
public JsonObjectMapper() {
super();
this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
}
}
When i POST i'm getting this error
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "notificacion" (class ar.com.tecnoaccion.services.notificacion.NotificacionDTO), not marked as ignorable (3 known properties: , "tipoNotificacion", "instalacion", "mensaje"])
at [Source: org.apache.catalina.connector.CoyoteInputStream@1c1f3f7; line: 3, column: 5] (through reference chain: ar.com.tecnoaccion.services.notificacion.NotificacionDTO["notificacion"])
I try to add this to my DTO
@JsonIgnoreProperties(ignoreUnknown = true)
but when i validate my DTO with the method validate all the dto's attributes are null and i get this error:
java.lang.RuntimeException: El parametro instalacion es null o no esta presente en el JSON.
i am using jackson 2.2.3 and spring 3.2.1
thank you.
Upvotes: 2
Views: 5559
Reputation: 2714
the simple answer is to post
{
"instalacion":"1",
"tipoNotificacion":"2",
"mensaje":"Un Mensaje"
}
instead of
{
"notificacion":
{
"instalacion":"1",
"tipoNotificacion":"2",
"mensaje":"Un Mensaje"
}
}
Upvotes: 2