2

I'm trying to send a POST request to Sprin boot with a list of custom objects in the body. My JSON in request body is this:

[{"name":"name1","icon":"icon1"},
{"name":"name2","icon":"icon2"},
{"name":"name3","icon":"icon3"}]

And I get this error

Cannot construct instance of `io.wedaily.topics.models.Topic` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

My controller:

@PostMapping
public void createTopics(@RequestBody List<Topic> topics) {
    System.out.println(topics);
}

My Topic Model:

public class Topic {

    private Long id;
    private String name;
    private String icon;
    private Date createdAt;
// Constructor
// Getters
// Setters
}

2 Answers 2

6

The exception is pretty explicit and tells you exactly what is going on. Jackson needs a default, no-args constructor defined with getters and setters for each field you want deserialized, OR, you need a constructor with Jackson annotations telling it how to map the json into your constructor.

Simply modify your topic class to include the default constructor like below. (If you use lombok annotating your class with @Data would also do the trick)

public class Topic {
 private Long id; 
 private String name; 
 private String icon; 
 private Date createdAt; 

 public Topic(){
 }

 // Other all args constructor
 // Getters
 // Setters 
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your application needs a model class to that Jackson can map your post data with some kind of object and then you can create a list of that object.

Like you are sending in post is

[{"name":"name1","icon":"icon1"},
{"name":"name2","icon":"icon2"},
{"name":"name3","icon":"icon3"}]

So your model class will be like

public class mapModel { 
    private String name;
    private String icon;

    public String getName(){return this.name;}
    public void setName(String name){this.name  = name;}

    public String getIcon(){return this.icon;}
    public void setIcon(String icon){this.icon  = icon;}
}

And your postMapping controller will look like this

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import com.doc.autobuild.autobuild.model.mapModel;

@RestController
public class postMappingExample{
    @PostMapping("/reqPost")
    public ResponseEntity<HttpStatus> postController(@RequestBody List<mapModel> bodyParamList){
        for(mapModel mm : bodyParamList){System.out.println(mm.getName());}
        return ResponseEntity.ok(HttpStatus.OK);
    }
}

Comments

Your Answer

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.