Getting null as a property value Json spring auto conversion

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
on
Hi,
From my js file there is an ajax call with the following data :
"{"title":"Title","html":"<div>Text</div><div><br></div>","message":"Message"}"

Open in new window

And i have a java class like :
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.springframework.web.util.HtmlUtils;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Note {

    private String id;
    private String title;
    @JsonProperty("html")
    private String body;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public void parseHtml() {
        body = HtmlUtils.htmlEscape(body);
        title = HtmlUtils.htmlEscape(title);
    }

}

Open in new window

The controller corresponding to this request is :
   public void createNote(@RequestBody Note note, HttpServletResponse response) throws IOException {


Using debugger i found that although the value for title in note class is getting set properly but i am getting body = null.

what could be reason for that ?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
Just thinking out loud (I'm not in a position to test this at the moment), perhaps because you have a setter method, it is being used rather than field injection and so the annotation is being missed.

Try copying the @JsonProperty annotation to the setter and getter methods and see if that resolves the issue. Also, if the setter method is not needed by your code, you could try removing it.

Author

Commented:
Still the same issue modified the code to :
 @JsonProperty("html")
    public String getBody() {
        return body;
    }

    @JsonProperty("html")
    public void setBody(String body) {
        this.body = body;
    }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial