如何在 Kotlin 中转换枚举请求?

huangapple 未分类评论48阅读模式
英文:

how to convert enum request in kotlin?

问题

我正在使用带有 Spring 的 Kotlin。我想要转换来自前端的请求。我的枚举类是:

enum class Type() {
    PARTIAL,
    FULL;

    fun equal(type: String) {
        return this.equal(type.toUpperCase())
    }
}

我的获取 API 请求如下:

fun updateUserInfo(user: User, @Valid @RequestBody request: RequestBody) {...}

我的请求体如下:

class RequestBody private constructor() {

    @NotNull
    lateinit var type: Type

    @NotBlank
    @Size(max = 40)
    lateinit var fullName: String
}

我的问题是,前端可能会使用小写,比如 partial,full。我希望无论前端发送什么,我的 Type 枚举在 updateUserInfo 函数中始终以大写形式呈现。我该如何做到这一点?

英文:

I am using Kotlin with spring. I want to convert request which comes from the front end. My enum class is:

enum class Type() {
    PARTIAL,
    FULL;

    fun equal(type:String){
        return this.equal(type.toUpperCase())
    }
}

My get api request is like this

fun updateUserInfo(user: User, @Valid @RequestBody request: RequestBody) {...}

My Request body is like this:

class RequestBody private constructor() {

    @NotNull
    lateinit var type: Type
    

    @NotBlank
    @Size(max = 40)
    lateinit var fullName: String
}

My question is this frontend might using small casing like partial,full. I want to add modifier irrespective of anything frontend send my Type enum should always give an upper case in updateUserInfo function. How can I do it?

huangapple
  • 本文由 发表于 2020年5月19日 18:52:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/61889272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定