Spring REST APi使用PagingAndSortingRepository搜索时返回空结果

时间:2019-06-06 23:03:32

标签: java spring rest api jpa

我正在尝试构建一个REST API,该API返回具有特定senderId的用户进行的所有交易的列表,但是在检索列表本身时遇到一些问题。 此版本适合于我的客户功能,可以将年龄作为senderId的替代者,因此我不明白为什么这会引起问题。 每当我使用PagingAndSortingRepository搜索时,我的页面都会返回一个空的JSON,如下所示:

{
  "_embedded" : {
    "transactions" : [ ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/transactions/search/findTransactionsBySenderId?senderid=1"
    }
  }
}

我有一个交易类:

@Entity
public class Transaction {


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonIgnore
    private Long transactionId;
    private Long senderId;
    private Account.AccountType senderAccountType;
    private Long recipientId;
    private double amountTransferred = 0;
    private Date transactionDate;

    public Transaction() {
    }

    // Getters & Setters

我有一个TransactionRepository:

public interface TransactionRepository extends PagingAndSortingRepository<Transaction, Long> {
    Iterable<Transaction> findTransactionsBySenderId(@Param("senderId") Long senderId);
}

还有一个TransactionController:

@RestController
public class TransactionController {

    // TransactionController
    @Autowired
    TransactionRepository transactionRepository;

    @GetMapping(path = "/transactions")
    public Iterable<Transaction> getTransactions() {
        return transactionRepository.findAll();
    }

    @PostMapping(path = "/transactions", consumes = {"application/json"})
    public Transaction createTransaction(@RequestBody Transaction transaction) {

        transactionRepository.save(transaction);
        return transaction;
    }
}

我的数据库看起来像这样: mysql database

0 个答案:

没有答案