#Spring boot post method returning http 500 because of jackson
13 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @lone cloud! Please use
/closeor theClose Postbutton 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.
Do you have a default constructor?
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?
Let me see the class and the endpoint
@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.
Do you see the code?
Does the error message say that User does not have a default constructor?
I found the error 🙂