0

I have problem with convert JSON to Java class.

Controller

@RequestMapping(value = "/{username}/add", method = POST)
 public void add(@RequestBody NoteModel note) {
 System.out.println(note.getTitle());
 }

JSON

{
 title : "Title",
 text : "Text"
}

NoteModel

public class NoteModel {
 private String title;
 private String text;
 public String getTitle() {
 return title;
 }
 public void setTitle(String title) {
 this.title = title;
 }
 public String getText() {
 return text;
 }
 public void setText(String text) {
 this.text = text;
 }
}

So, when I send json to the controller, Controller see same url, but can't deserialize JSON to Java (I think). Because, when I try, to send JSON - { title : "Title" }, and controller wait argument - @RequestBody String note, it can easily display it.

I'm try to do, what was in https://gerrydevstory.com/2013/08/14/posting-json-to-spring-mvc-controller/ and include adapter in servlet.xml, but was the same effect.

AJAX

$.ajax({
 type : "POST",
 contentType : "application/json; charset=utf-8",
 url : window.location.pathname,
 data : JSON.stringify({
 title : $("#titleId").val(),
 text : $("#textId").val()
 }),
 success: function () {
 $("#titleId").val("");
 $("#textId").val("");
 }
 })
asked Aug 29, 2016 at 11:01
9
  • How do you send the JSON? Did you set the content-type to application/json? Commented Aug 29, 2016 at 11:04
  • yes, the content type is, wait some time, I add ajax code Commented Aug 29, 2016 at 11:06
  • have you added dependency for json in pom.xml? Commented Aug 29, 2016 at 12:33
  • yes, mapper-asl and also core-asl Commented Aug 29, 2016 at 12:35
  • <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.4</version> </dependency> also <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20151123</version> </dependency> and try once Commented Aug 29, 2016 at 12:38

3 Answers 3

1

Add @RequestMapping(value = "/{username}/add", method = POST, produces = "application/json")

answered Aug 29, 2016 at 11:11
Sign up to request clarification or add additional context in comments.

2 Comments

added, but again, I have same effect
Sorry earlier I misunderstood the question. Send the data as below. data : { title : $("#titleId").val(), text : $("#textId").val() }
0

Make sure that you have added content-type to "application/json" in header of your request.

answered Aug 29, 2016 at 11:06

1 Comment

I try to do it, but it's not change situation gerrydevstory.com/2013/08/14/…
0

how to catch the problem: Send the String to your controller and try to create your object. Put breakpoint to objectMapper.readValue() and check what is the exactly problem;

@RequestMapping(value = "/{username}/add", method = POST)
public void add(@RequestBody String note) {
 ObjectMapper objectMapper = new ObjectMapper();
 NoteModel noteModel = objectMapper.readValue(result, NoteModel.class);
}

I think that there is some conflict between default ObjectMapper and JSON mapper logic.

answered May 24, 2018 at 11:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.