1

I have a Spring application with the following controller:

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/office")
public class OfficeController {

    private OfficeRepository officeRepository;

    public OfficeController(OfficeRepository officeRepository){

        this.officeRepository = officeRepository;
    }

    @GetMapping("/all")
    public List<Office> getAll(){
        List<Office> offices = this.officeRepository.findAll();

        return offices;
    }

    @PutMapping
    public void insert(@RequestBody Office office){

        this.officeRepository.insert(office);
    }

    @PostMapping
    public void update(@RequestBody Office office){

        this.officeRepository.save(office);
    }

    @GetMapping("/{id}")
    public Office getByIdOffice(@PathVariable("id") String id){
        Office office = this.officeRepository.findById(id);

        return office;
    }

    @GetMapping("/status/{status}")
    public List<Office> getByStatus(@PathVariable("status") String status){
        List<Office> offices = this.officeRepository.findByStatus(status);

        return offices;
    }

    @GetMapping("/floor/{floor}")
    public List<Office> getByFloor(@PathVariable("floor") String floor){
        List<Office> offices = this.officeRepository.findByFloor(floor);

        return offices;
    }
}

And secondary class is:

public class User {
    //@Id
    //private String id;
    private String firstName;
    private String lastName;

    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    // public String getId() {
    //     return id;
    // }

    public String getFirstName() {

        return firstName;
    }

    public String getLastName(){

        return lastName;
    }

}

However, when I send a POST request from Postman containing :

public void run(String... strings) throws Exception {
        Office bir = new Office(
                "2",
                "busy",
                "12/07/2019",
                "20/07/2019",
                Arrays.asList(
                        new User("Jon", "Snow"))
        );

I get the following response:

{
    "timestamp": 1562918471785,
    "status": 400,
    "error": "Bad Request",
    "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
    "message": "Could not read document: Can not construct instance of office.demo.User: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@85534e1; line: 8, column: 17] (through reference chain: office.demo.Office[\"users\"]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of office.demo.User: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@85534e1; line: 8, column: 17] (through reference chain: office.demo.Office[\"users\"]->java.util.ArrayList[0])",
    "path": "/office"
}

Also, I was experimenting with Spring Security as well. The server does not show any error and the controller does not seem to receive the request as "inside" is not being printed. I am trying to get acquainted with Spring, however I could not find the reason for such an error. I would be grateful for any help. All other methodes like /all are all working perfectly fine, I tried to make another seeder but that dosen't work to .Thanks in advance.

  • I found you have no setters in User class and may be @JsonCreator annotation for the constructor will work. Incase - https://stackoverflow.com/questions/21920367/why-when-a-constructor-is-annotated-with-jsoncreator-its-arguments-must-be-ann – Rohith K Jul 12 '19 at 08:24

1 Answers1

1

The error message is clear, User is missing default constructor. If you declare any constructor explicitly then you are responsible for adding No Arg constructor. Because by default jackson uses setters and getters for serialization and deserialization

public class User {
//@Id
//private String id;
private String firstName;
private String lastName;

public User(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
}

public User() {
   }

// public String getId() {
//     return id;
// }

public String getFirstName() {

    return firstName;
}

public String getLastName(){

    return lastName;
    }

 }
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98