Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on 

Getting null as a property value Json spring auto conversion

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
JavaJava EE

Avatar of undefined
Last Comment
Rohit Bajaj
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Rohit Bajaj
Rohit Bajaj
Flag of India image

ASKER

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

Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo