Reputation: 358
I try to convert following method to Kotlin but don't know the equivalent syntax. The full class can be found here. Not sure we can implement this in kolin.
@GetMapping("/account")
@Transactional(propagation = REQUIRES_NEW)
public HttpEntity<PagedModel<AccountModel>> listAccounts(
@PageableDefault(size = 5, direction = Sort.Direction.ASC) Pageable page) {
return ResponseEntity
.ok(pagedResourcesAssembler.toModel(accountRepository.findAll(page), accountModelAssembler()));
}
private RepresentationModelAssembler<Account, AccountModel> accountModelAssembler() {
return (entity) -> {
AccountModel model = new AccountModel();
model.setName(entity.getName());
model.setType(entity.getType());
model.setBalance(entity.getBalance());
model.add(linkTo(methodOn(AccountController.class)
.getAccount(entity.getId())
).withRel(IanaLinkRelations.SELF));
return model;
};
}
Upvotes: 0
Views: 207
Reputation: 5364
Here is your code in Kotlin:
@GetMapping("/account")
@Transactional(propagation = Propagation.REQUIRES_NEW)
fun listAccounts(@PageableDefault(size = 5, direction = Direction.ASC) page: Pageable?):
HttpEntity<PagedModel<AccountModel?>?>? {
return ResponseEntity
.ok(pagedResourcesAssembler.toModel(accountRepository.findAll(page), accountModelAssembler()))
}
private fun accountModelAssembler(): RepresentationModelAssembler<Account, AccountModel> {
return RepresentationModelAssembler<Account, AccountModel> { entity: Account ->
val model = AccountModel()
model.name = entity.name
model.type = entity.type
model.balance = entity.balance
model.add(linkTo(methodOn(AccountController::class.java).getAccount(entity.id)).withRel(IanaLinkRelations.SELF))
model
}
}
Of course, I am not aware of some of your classes, so there is a chance it won't compile. Some things are very close to Java, but to understand others you need to be familiar with Kotlin :)
Upvotes: 2
Reputation: 3298
For me the shortest way is to copy the Java code and paste it in Kotlin file of the IDE. And the IDE (Android Studio / IntelliJ) will do the rest. This might face some issues to convert the code. For that case you have to fix those manually.
Upvotes: 1
Reputation: 5223
To convert java code into kotlin code in a fast way, you need to open the .java file that you want to convert to kotlin.
Go to Code(menu) / click on Convert Java File to Kotlin File
This will automatically convert your java code into kotlin code.
Upvotes: 1