ObjectMapper not considering annotations

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


ObjectMapper not considering annotations



I have the below piece of code which would convert an annotated Java object into Json String.


try {
String jsonString = mapper.writeValueAsString(obj);
LOGGER.debug("logAsJson", jsonString);
} catch (Exception e) {
LOGGER.warn("logAsJson", "Exception in logging only. Nothing critical! ", e);
}



And my class would look something like this



public class Car {


@JsonProperty("capabilities")
private List<Capability> capability = new ArrayList<Capability>();

@JsonProperty("periodicity")
@NotNull
private Periodicity periodicity;

@JsonProperty("car_driver")
private List<carDriver> carDriver = new ArrayList<carDriver>();



}



Problem is that while creating the json string from the object, object mapper is not considering the annotations for field names.



Thanks in advance.





Check if you have jackson 1 and jackson 2 in your classpath, and if you're using mapper from one version and annotations from the other one.
– Federico Peralta Schaffner
Sep 18 '15 at 19:49





That was my thought also, check the annotation you are using matches the library you are using.
– Peter Lawrey
Sep 18 '15 at 19:50







That worked Guys, Thanks a lot :)
– Ajay
Sep 18 '15 at 20:12





@Ajay How did you fixed this issue ?. I am also facing the same prob
– Abdul Razak AK
yesterday




1 Answer
1



The json annotation and the object mapper may not be from the same library. Make sure you have imported the same library in both the files.



Example,



import com.fasterxml.jackson.annotation.JsonProperty;


import com.fasterxml.jackson.annotation.JsonProperty;



and


import com.fasterxml.jackson.databind.ObjectMapper;






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

415 Unsupported Media Type while sending json file over REST Template

PHP parse/syntax errors; and how to solve them?