【问题标题】:Sending post request to @RestController method in spring boot with nested json in request使用请求中的嵌套 json 在 Spring Boot 中向 @RestController 方法发送 post 请求
【发布时间】:2021-10-07 22:49:26
【问题描述】:

我是 Spring boot 的新手,正在尝试创建 rest API。我创建了一个与客户实体类具有ma​​nytomany关系的帐户实体类。

@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

这是我用于发布请求的控制器类方法

 @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);
    }

}

上面代码中AccountServices类如下

@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();
    }

和AccountRepository如下-

public interface AccountRepository extends CrudRepository<Account, Integer> {
//custom finder method to get book record with id
}

我发送这个帖子请求来检查我的处理方法postAccount

但我在邮递员中遇到错误

这里的错误是什么? 我想发送请求中的相同对象并将其打印为邮递员的响应。

控制台上的错误是 -

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

【问题讨论】:

  • 嘿......在邮递员中发出请求时将类型更改为JSON............主体是原始的,类型是JSON,...... .我可以在上面的图片中看到文字......并且错误消息也在说......仔细看看......

标签: java spring spring-boot rest


【解决方案1】:

你在这里错过了逗号(,)。把它。

【讨论】:

    【解决方案2】:

    change Type in Postman from text to json

    【讨论】:

      【解决方案3】:

      客户字段前缺少逗号- 正确的是

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

      【讨论】:

        猜你喜欢
        • 2015-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-05
        • 2020-05-09
        • 1970-01-01
        相关资源
        最近更新 更多