Skip to content

Instantly share code, notes, and snippets.

@mchlstckl
mchlstckl / Property.kt
Last active August 4, 2023 07:37
Example entity with composite key Spring Boot and Kotlin
// This will not serialize!
data class Muppet(val name: String)
// This will serialize!
data class Puppet(val name: String = "")
// Composite key class must implement Serializable
// and have defaults.
class PropertyId(
val uuid: UUID = UUID.randomUUID(),
@mchlstckl
mchlstckl / Logging.kt
Created January 7, 2016 15:22
Log helper file for Kotlin
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.reflect.KClass
// Based on answer by Jayson Minard on Stack Oveflow question
// "Idiomatic logging for Kotlin"
// See: http://stackoverflow.com/questions/34416869
// Return logger for Java class, if companion object fix the name
public fun <T : Any> logger(forClass: Class<T>): Logger {
@mchlstckl
mchlstckl / UserService.kt
Last active January 7, 2016 19:22
HystrixCommand returning Observable
//...elided code...//
@HystrixCommand(/* ...some very advanced hystrix configurations here... */)
open fun fetchUser(email: String): Observable<User> {
// MUST return ObservableResult when using @HystrixCommand and Observable<T>
// See: https://github.com/Netflix/Hystrix/issues/729
return object : ObservableResult<User>() {
override fun invoke() = client.fetchUser(email)
}
}
@mchlstckl
mchlstckl / ProfileController.kt
Last active January 7, 2016 17:01
Async Reactive operation with fallback observable
//...elided package, class and imports...//
@RequestMapping("/profiles", method = arrayOf(RequestMethod.GET))
fun getUserByEmail(
@RequestParam(value = "email") email: String,
): DeferredResult<ResponseEntity<Profile>> {
val result = DeferredResult<ResponseEntity<Profile>>()
val uriBuilder = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{uuid}")
@mchlstckl
mchlstckl / ProfileController.kt
Last active January 7, 2016 15:34
Example logging with Kotlin
// ...elided package and imports... //
import com.example.profileservice.logging.logger
@RestController
class ProfileController @Autowired constructor(
val profileRepository: ProfileRepository
) {
// This is how you create a logger
val log = logger()
@mchlstckl
mchlstckl / build.gradle
Created January 7, 2016 13:21
Example build.gradle file for Spring Boot with Kotlin
group 'com.example.profile'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.0.0-beta-3595'
ext.spring_boot_version = '1.3.1.RELEASE'
ext.spring_dmp_version = '0.5.2.RELEASE'
ext.spring_cloud_netflix_version = '1.0.4.RELEASE'
ext.rx_kotlin_version = '0.30.1'