Mayank Kumar Thakur
Mayank Kumar Thakur

Reputation: 704

Sending post request to @RestController method in spring boot with nested json in request

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 enter image description here

but I am getting error in postman enter image description here

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

Answers (3)

Geeth
Geeth

Reputation: 554

You missed comma(,) in here . put it .enter image description here

Upvotes: 3

Mayank Kumar Thakur
Mayank Kumar Thakur

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

Abdalrhman Alkraien
Abdalrhman Alkraien

Reputation: 808

enter image description herechange Type in Postman from text to json

Upvotes: 1

Related Questions