我正在创建一个API rest,当我执行“ find ALL”而不是获取所有数据时,我只需要获取其中的一部分。使用Java,我是这样做的:
@GetMapping(value = "/pagina")
public ResponseEntity<Page<PersonFindDTO>> findAll(
@RequestParam(value = "page", defaultValue = "0") Integer page,
@RequestParam(value = "linesPerPage", defaultValue = "24") Integer linesPerPage,
@RequestParam(value = "order", defaultValue = "ASC") String order,
@RequestParam(value = "orderBy", defaultValue = "name") String orderBy) {
var person = personService.findAll(page, linesPerPage, order, orderBy);
var personFindDto = person.map(PersonFindDTO::new);
return ResponseEntity.ok().body(personFindDTO);
}
与Kotlin一起,我正在尝试这种方式:
@GetMapping(value = ["/{companyId}/{active}"])
override fun findAll(
@RequestParam(value = "page", defaultValue = "0") page: Int,
@RequestParam(value = "linesPerPage", defaultValue = "24") linesPerPage: Int,
@RequestParam(value = "order", defaultValue = "ASC") order: String,
@RequestParam(value = "orderBy", defaultValue = "tradeName") orderBy: String,
@PathVariable companyId: Long, @PathVariable active: Boolean): ResponseEntity<Page<Any>> {
val lp = service.findAll(page, linesPerPage, order, orderBy, companyId, active)?.let {
it.map {
fun LegalPerson.toLegalPersonMPage() = LegalPersonMPage(id = it.id,
tradeName = it.tradeName, companyName = it.companyName, cnpj = it.cnpj)
}
}
return ResponseEntity.ok().body(lp)
}
但是返回总是空的。有人可以帮忙吗?拜托。
更新:
我的LegalPerson班
data class LegalPerson(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
override var id: Long,
@Column(nullable = false)
val companyId: Long,
@Column(nullable = false)
var active: Boolean,
@Column(nullable = false, length = 100)
val tradeName: String,
@Column(nullable = false, length = 100)
val companyName: String,
@Column(nullable = false, length = 100)
val email: String,
@Column(nullable = false, length = 18)
val cnpj: String,
@Column(length = 15)
val stateRegistration: String,
@Column(length = 15)
val municipalRegistration: String,
@Column(nullable = false)
val openingDate: LocalDate,
@Column(nullable = false)
val address: Long,
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "phone", schema = "legal_person")
val phones: List<Long>
)
我的LegalPersonMPage类
data class LegalPersonMPage(
val id: Long,
val tradeName: String,
val companyName: String,
val cnpj: String
) {
}
答案 0 :(得分:1)
函数LegalPerson.toLegalPersonMPage()
已定义但从未调用。为了使这一点更加清楚,请在map {...}
外部定义函数,然后在map {...}
内部对其进行调用。
fun LegalPerson.toLegalPersonMPage() = LegalPersonMPage(id = this.id,
tradeName = this.tradeName, companyName = this.companyName, cnpj = this.cnpj)
@GetMapping(value = ["/{companyId}/{active}"])
override fun findAll(
@RequestParam(value = "page", defaultValue = "0") page: Int,
@RequestParam(value = "linesPerPage", defaultValue = "24") linesPerPage: Int,
@RequestParam(value = "order", defaultValue = "ASC") order: String,
@RequestParam(value = "orderBy", defaultValue = "tradeName") orderBy: String,
@PathVariable companyId: Long, @PathVariable active: Boolean): ResponseEntity<Page<Any>> {
val lp = service.findAll(page, linesPerPage, order, orderBy, companyId, active)?.let {
it.map { legalPerson -> legalPerson.toLegalPersonMPage() }
}
return ResponseEntity.ok().body(lp)
}