#Spring boot post method returning http 500 because of jackson

13 messages · Page 1 of 1 (latest)

lone cloud
#

Hi. I have made a springboot api which have a post-method that takes in a list of users as request body, but when I try the api it returns 500 internal server error and it tells me that Jackson cannot find default contructor.

spare gulchBOT
#

This post has been reserved for your question.

Hey @lone cloud! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

wooden widget
#

Do you have a default constructor?

lone cloud
#

yes ive tried to make a default constructor and ive tried labeling everything with jsonProperty and jsonCreator, but i get the same error

#

Might it have something to do with version conflicts or that they lie in seperate modules or somethiong?

wooden widget
#

Let me see the class and the endpoint

quick gazelleBOT
#

    @JsonProperty("username")
    private String username;
    @JsonProperty("balance")
    private int balance;
    @JsonProperty("password")
    private String password;

    @JsonCreator
    public User() {

    }

    // Constructor for a new user with a default balance of 1000
    @JsonIgnore
    public User(String username, String password) {
        this(username, 1000, password);
    }

    // Constructor for a user with a custom balance
    @JsonCreator
    public User(@JsonProperty("username") String username, @JsonProperty("balance") int balance,
            @JsonProperty("password") String password) {
        if (balance < 0) {
            throw new IllegalArgumentException("Balance cannot be negative");
        }
        if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
            throw new IllegalArgumentException("Username or password is empty");
        }
        this.username = username;
        this.balance = balance;
        this.password = password;
    }

    @JsonProperty("username")
    public String getUsername() {
        return username;
    }

    @JsonProperty("password")
    public String getPassword() {
        return password;
    }

    @JsonProperty("balance")
    public int getBalance() {
        return balance;
    }

    @JsonProperty("username")
    public void setUsername(String username) {
        this.username = username;
    }

    @JsonProperty("balance")
    public void setBalance(int balance) {
        this.balance = balance;
    }

    @JsonProperty("password")
    public void setPassword(String password) {
        this.password = password;
    }} ```

This message has been formatted automatically. You can disable this using /preferences.

#

Endpoint: @PostMapping("/saveUsers") public ResponseEntity<String> saveUsers(@RequestBody List<User> users) { if (users == null) { return ResponseEntity.badRequest().body("Users is null"); } users.forEach(user -> { this.createUserFile(user); }); return ResponseEntity.status(HttpStatus.CREATED).body("Users saved"); }

This message has been formatted automatically. You can disable this using /preferences.

#
    String filePath = this.getJsonFilePath(false, user.getUsername());
    File file = new File(filePath);

    if (!file.exists()) {
      try {
        file.createNewFile();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    try (FileWriter writer = new FileWriter(filePath);) {
      String jsonUser = new Gson().toJson(user);
      writer.write(jsonUser);
    } catch (IOException e) {
      e.printStackTrace();
    }
  } ```

This message has been formatted automatically. You can disable this using /preferences.

lone cloud
#

Do you see the code?

wooden widget
#

Does the error message say that User does not have a default constructor?

lone cloud
#

I found the error 🙂