0

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]]

1
  • 2
    Hey...change the type to JSON while making a request in postman............the body is raw and the type is JSON,......I can see text in the above pic.....and the error msg is also saying it.....pay a closer look... Commented Aug 2, 2021 at 15:24

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

enter image description herechange Type in Postman from text to json

Comments

1

Missed comma before customer field- Correct one is

{
    "accountNumber": "100",
    "accountType": "saving",
    "balance": "23000",
    "customers": [
        {
            "customerId": "2300",
            "firstName": "mayank",
            "lastName": "Kumar",
            "email": "[email protected]"
        }
    ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.