我目前正在搞弄一个Spring Webflux微服务,并试图实现一个@PatchMapping,它使用一个JsonPatch对象作为@RequestBody,如下所示:

@PatchMapping("/{id}", consumes = ["application/json-patch+json"])
fun updateFriend( @PathVariable("id") id: Long,
                  @Valid @RequestBody jsonPatch: JsonPatch): Mono<UserFriends> {
    // apply JsonPatch code here...
}

我用Postman请求测试了端点,如下所示:
PATCH <microservice-uri>/friends/1
Content-Type = application/json-patch+json
RequestBody =
[
    {
        "op": "replace",
        "path": "friends/11/since",
        "value": "<sample value>"
    }
]

这是我得到的异常(exception):
org.springframework.core.codec.CodecException: Type definition error: [simple type, class javax.json.JsonPatch]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `javax.json.JsonPatch` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (io.netty.buffer.ByteBufInputStream); line: 1, column: 1]
    at org.springframework.http.codec.json.AbstractJackson2Decoder.processException(AbstractJackson2Decoder.java:211) ~[spring-web-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
    |_ checkpoint ⇢ HTTP PATCH "/friends/1" [ExceptionHandlingWebHandler]
<Additional stacktraces here ...>

我很清楚这一事实,我需要告诉微服务如何将RequestBody转换为JsonPatch。但是失败了三天后,我决定寻求帮助。

经过多次失败的尝试后,我找到了WebFluxConfigurer并尝试覆盖configureArgumentResolvers。为JsonPatch编写HandlerMethodArgumentResolver时,我陷入了困境。
class JsonPatchResolver : HandlerMethodArgumentResolver {
    override fun supportsParameter(parameter: MethodParameter): Boolean {
        // Don't know what to implement here
    }

    override fun resolveArgument(parameter: MethodParameter, bindingContext: BindingContext, exchange: ServerWebExchange): Mono<Any> {
        // Don't know how to create a JsonPatch from the given arguments
    }
}

有人可以指出正确的方向或告诉我我做错了什么吗?

提前非常感谢您!

编辑
这是我的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>friend-info-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>friend-info-service</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <kotlin.version>1.3.72</kotlin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>
        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.java-json-tools</groupId>
            <artifactId>json-patch</artifactId>
            <version>1.12</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-kotlin</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr353</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.json</groupId>
            <artifactId>javax.json-api</artifactId>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
        <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <configuration>
                    <args>
                        <arg>-Xjsr305=strict</arg>
                    </args>
                    <compilerPlugins>
                        <plugin>spring</plugin>
                    </compilerPlugins>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-allopen</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>


编辑2
以下内容不起作用:
class JsonPatchArgumentResolver : HandlerMethodArgumentResolver {

    override fun supportsParameter(parameter: MethodParameter): Boolean {
        // this doesn't event get called when debugging, so I suspect the handler isn't registered correctly
        return parameter.parameterType == JsonPatch::class
    }

    override fun resolveArgument(parameter: MethodParameter, bindingContext: BindingContext, exchange: ServerWebExchange): Mono<Any> {
        return exchange.request.body.toMono().map {
            jacksonObjectMapper().readValue(it.asInputStream(), JsonPatch::class.java)
        }
    }

}

在我的应用程序类中,我正在注册它(可能不正确,我不知道):
@SpringBootApplication
@EnableReactiveMongoRepositories
@EnableWebFlux
@Configuration
class FriendInfoServiceApplication : WebFluxConfigurer {
    override fun configureArgumentResolvers(configurer: ArgumentResolverConfigurer) {
        configurer.addCustomResolver(JsonPatchArgumentResolver())
    }
}

fun main(args: Array<String>) {
    runApplication<FriendInfoServiceApplication>(*args)
}

编辑3
在调试时,我发现configureArgumentResolvers被调用了,所以也许我对supportsParameter做错了什么?

编辑4
我尝试从THIS博客文章中复制代码。 kotlin等效项应为:

@Component
class JsonPatchHttpMessageConverter : AbstractHttpMessageConverter<JsonPatch>() {

    @Throws(HttpMessageNotReadableException::class)
    protected override fun readInternal(clazz: Class<out JsonPatch>, inputMessage: HttpInputMessage): JsonPatch {
        try {
            Json.createReader(inputMessage.body).use { reader -> return Json.createPatch(reader.readArray()) }
        } catch (e: Exception) {
            throw HttpMessageNotReadableException(e.message!!, inputMessage)
        }
    }

    @Throws(HttpMessageNotWritableException::class)
    protected override fun writeInternal(jsonPatch: JsonPatch, outputMessage: HttpOutputMessage) {
        throw NotImplementedError("The write Json patch is not implemented")
    }


    protected override fun supports(clazz: Class<*>): Boolean {
        return JsonPatch::class.java.isAssignableFrom(clazz)
    }
}

并将objectMapper添加到Application类中,如下所示:
@SpringBootApplication
@EnableReactiveMongoRepositories
@EnableWebFlux
@Configuration
class FriendInfoServiceApplication {
    @Bean
    fun objectMapper(): ObjectMapper {
        val objectMapper = ObjectMapper()
        objectMapper.registerModule(JSR353Module())
        return objectMapper
    }
}

可悲的是,尝试调用PATCH端点时,结果是相同的异常。

最佳答案

我是您提到的博客文章的作者。我不是Webflux的专家,但是在这种情况下,您必须将JacksonConverter重新实现为webFlux表单。

这样就变成了这样的东西:

import org.springframework.core.ResolvableType
import org.springframework.core.io.buffer.DataBuffer
import org.springframework.core.io.buffer.DataBufferUtils
import org.springframework.http.MediaType
import org.springframework.http.ReactiveHttpInputMessage
import org.springframework.http.codec.HttpMessageReader
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import javax.json.Json
import javax.json.JsonPatch

class JsonPatchHttpMessageConverter : HttpMessageReader<JsonPatch> {
    override fun getReadableMediaTypes(): List<MediaType> {
        return listOf(MediaType.valueOf("application/json-patch+json"))
    }

    override fun canRead(elementType: ResolvableType, mediaType: MediaType?): Boolean {
        return MediaType.valueOf("application/json-patch+json").includes(mediaType)
    }

    override fun read(elementType: ResolvableType, message: ReactiveHttpInputMessage, hints: Map<String, Any>): Flux<JsonPatch> {
        //TODO implement the same mono logic here
        return Flux.empty();
    }

    override fun readMono(elementType: ResolvableType, message: ReactiveHttpInputMessage, hints: Map<String, Any>): Mono<JsonPatch> {
        return DataBufferUtils.join(message.body).map { buffer: DataBuffer ->
            //TODO error handling
            val reader = Json.createReader(buffer.asInputStream())
            Json.createPatch(reader.readArray())
        }
    }
}

请注意,它尚未完全实现,您必须执行错误处理并实现read(elementType: ResolvableType, message: ReactiveHttpInputMessage, hints: Map<String, Any>): Flux<JsonPatch>

现在,您必须注册此自定义解码器。在 Spring 的webFlux中,您可以通过创建WebFluxConfigurer bean来做到这一点:
@Bean
fun webFluxConfigurer(): WebFluxConfigurer {
    return object : WebFluxConfigurer {
        override fun configureHttpMessageCodecs(configurer: ServerCodecConfigurer) {
            configurer.customCodecs().register(JsonPatchHttpMessageConverter())
        }
    }
}

最后是路由器和处理程序:
@Configuration
class GreetingRouter {
    @Bean
    fun route(greetingHandler: GreetingHandler): RouterFunction<ServerResponse> {
        return RouterFunctions
                .route(RequestPredicates.POST("/hello")
                        .and(RequestPredicates.accept(MediaType.valueOf("application/json-patch+json"))), HandlerFunction { request: ServerRequest? -> greetingHandler.hello(request!!) })
    }

}


@Component
class GreetingHandler {
    fun hello(request: ServerRequest): Mono<ServerResponse> {
        return request.bodyToMono(JsonPatch::class.java)
                .flatMap { jsonPatch: JsonPatch ->
                    ServerResponse.ok().contentType(MediaType.valueOf("application/json-patch+json"))
                            .body(BodyInserters.fromValue("Received: $jsonPatch"))
                }
    }
}

最后,您可以使用curl调用此端点,例如:
curl -X POST \
  http://localhost:8080/hello \
  -H 'content-type: application/json-patch+json' \
  -d '[
   {
      "op":"replace",
      "path":"/email",
      "value":"[email protected]"
   }
]'

10-08 03:06