Resource
An annotation for classes that act as typed routes. All annotated types should be Serializable.
Every property that has a corresponding placeholder inside path is used as a value for this placeholder. Other properties are put into the URL query. Example:
@Serializable
@Resource("/users/{id}")
data class UserById(val id: Long, val properties: List<String>)
val userById = UserById(id = 123, properties = listOf("name", "avatar"))
val url = href(userById)
assertEquals("/users/123?properties=name&properties=avatar")
Content copied to clipboard
Properties can be primitives or types annotated with the Serializable annotation.
You can nest class for better organization, but all nested classes should have a property with an outer class type. Example:
@Serializable
@Resource("/users")
data class Users {
@Serializable
@Resource("/{id}")
data class ById(val parent: Users = Users(), val id: Long)
@Serializable
@Resource("/add")
data class Add(val parent: Users = Users(), val name: String)
}
val userById = Users.ById(123)
val addUser = Users.add("new_name")
Content copied to clipboard