Skip to content

[Kotlin] ForyJsonException occurs when integrating ForyJsonKotlin with Spring #4053

Description

@li-fcb1899

Search before asking

  • I had searched in the issues and found no similar issues.

Version

fory-json-kotlin 1.7.3
OS: Windows 11 25H2
Jdk 26.0.2.1
Kotlin 2.4.20

Component(s)

Java, Other

Minimal reproduce step

Result wrapper class:

data class Rt<T>(val flag: Boolean, val data: T? = null, val msg: String? = null)

fun <T> success(data: T): Rt<T> = Rt(true, data)

When using AbstractHttpMessageConverter, an error is reported: ForyJsonException: Unresolved Kotlin JSON type parameter 0

@Configuration
class WebConfig : WebMvcConfigurer {
    @get:Bean
    val foryJson: ForyJson = ForyJsonKotlin.builder().registerMixin<WithDetailMixin>().writeLongAsString(true)
        .defaultPropertyInclusion(NON_EMPTY).build()

    override fun configureMessageConverters(builder: HttpMessageConverters.ServerBuilder) {
        builder.withJsonConverter(
            object : AbstractHttpMessageConverter<Any>(APPLICATION_JSON) {
                override fun supports(clazz: Class<*>): Boolean = true
                override fun readInternal(clazz: Class<out Any>, inputMessage: HttpInputMessage): Any =
                    foryJson.fromJson(inputMessage.body.readAllBytes(), clazz)

                override fun writeInternal(t: Any, outputMessage: HttpOutputMessage) =
                    foryJson.writeJsonTo(t, outputMessage.body)
            })
    }
}

When using AbstractGenericHttpMessageConverter, an error is reported: ForyJsonException: Platform-typed Kotlin JSON occurrence java.util.List<com.reggie.entity.Employee> is unsupported

    override fun configureMessageConverters(builder: HttpMessageConverters.ServerBuilder) {
        builder.withJsonConverter(
            object : AbstractGenericHttpMessageConverter<Any>(APPLICATION_JSON) {
                override fun supports(clazz: Class<*>): Boolean = true
                override fun readInternal(clazz: Class<out Any>, inputMessage: HttpInputMessage): Any =
                    foryJson.fromJson(inputMessage.body.readAllBytes(), clazz)

                override fun writeInternal(t: Any, type: Type?, outputMessage: HttpOutputMessage) =
                    foryJson.writeJsonTo(t, TypeRef.of(type), outputMessage.body)

                override fun read(type: Type, contextClass: Class<*>?, inputMessage: HttpInputMessage): Any =
                    foryJson.fromJson(inputMessage.body.readAllBytes(), TypeRef.of(type))
            })

In the subclass methods of HttpMessageConverter, I don't know how to use jsonTypeRef<T>().
However, using only ForyJson for construction can achieve normal serialization.

What did you expect to see?

Serialize Rt<List> to JSON.

What did you see instead?

Constructed only using ForyJson.

@get:Bean
    val foryJson: ForyJson = ForyJson.builder().registerMixin<WithDetailMixin>().writeLongAsString(true)
        .defaultPropertyInclusion(NON_EMPTY).build()

Anything Else?

No response

Are you willing to submit a PR?

  • I'm willing to submit a PR!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions