Reputation: 704
I am new to Spring boot and trying to created rest APIs. I have a created a account entity class with manytomany relationship with customers entity class.
@Entity
@Table(name = "Account")
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int accountNumber;
public String accountType;
public int balance;
@ManyToMany(cascade = CascadeType.ALL)
private List<Customer> customers;
}
//I have skipped setters, getters, constructors and tostring method to reduce length
This is my controller class method for post request
@RestController
public class TestController {
@Autowired
private AccountServices accountServices;
private List<Account> allBooks;
@PostMapping("/accounts")
public ResponseEntity<Account> postAccount(@RequestBody Account account) {
Account ac = this.accountServices.addAccount(account);
return new ResponseEntity<>(ac, HttpStatus.OK);
}
}
In above code AccountServices class is as follows
@Component
public class AccountServices {
@Autowired
private AccountRepository accountRepository;
private Account save;
// get all books
public List<Account> getAllAccounts() {
System.out.println("Fetching all Accounts");
return (List<Account>) this.accountRepository.findAll();
}
and AccountRepository is as follows -
public interface AccountRepository extends CrudRepository<Account, Integer> {
//custom finder method to get book record with id
}
I am sending this post request to check my handler method postAccount
but I am getting error in postman
What is the error here? I want to send the same object that comes in request and print it as a response in postman.
Error on console is -
Fetching all Accounts
[]2021-08-02 21:47:32.620 WARN 20752 --- [nio-8081-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('"' (code 34)): was expecting comma to separate Object entries; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('"' (code 34)): was expecting comma to separate Object entries
at [Source: (PushbackInputStream); line: 5, column: 6]]
Upvotes: 0
Views: 2002
Reputation: 704
Missed comma before customer field- Correct one is
{
"accountNumber": "100",
"accountType": "saving",
"balance": "23000",
"customers": [
{
"customerId": "2300",
"firstName": "mayank",
"lastName": "Kumar",
"email": "[email protected]"
}
]
}
Upvotes: 1