Content-Type 'application/json;charset=UTF-8' is not supported异常解决

Content-Type 'application/json;charset=UTF-8' is not supported异常解决

前提:确定不是因为Content-Type导致的异常,controller层有注解@RequestBody。

报错详情:

image-20240522102453237

确定不是因为缺少Jackson依赖或者版本过低:

image-20240522102815355

注意到报错信息上边有一条警告日志:

.c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.xxx.ddd.api.OpenApiRequest<com.xxx.admin.api.insuranceProductConfig.models.request.UpdateComponentByComponentCodeReq>]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Failed to call `setAccess()` on Constructor 'java.util.Objects' (of class `java.util.Objects`) due to `java.lang.reflect.InaccessibleObjectException`, problem: Unable to make private java.util.Objects() accessible: module java.base does not "opens java.util" to unnamed module @77eca502

显而易见是因为执行反序列化出错,看到module java.base does not "opens java.util" to unnamed module基本可以确定是jdk17引起的问题,在jvm启动参数加上--add-opens java.base/java.util=ALL-UNNAMED后,发现真正报错原因如下:

image-20240522142741526
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.Objects` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('map')
 at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 56, column: 16] (through reference chain: com.xxx.ddd.api.OpenApiRequest["requestParams"]->com.xxx.admin.api.insuranceProductConfig.models.request.UpdateComponentByComponentCodeReq["components"]->java.util.ArrayList[0]->com.xxx.admin.api.insuranceProductConfig.models.valueObject.config.ComponentItem["context"]->java.util.LinkedHashMap["map"])

经排查定位到入参对象引用的一个java对象,在网上找解决方法是增加一个readValue的构造函数,如:https://blog.csdn.net/qq_30162239/article/details/86647164,要么对不想序列化的field加@JsonIgnore注解。


本博客内容仅供个人学习使用,禁止用于商业用途。转载需注明出处并链接至原文。

posted @ 2024-05-25 16:38  爱吃麦辣鸡翅  阅读(1608)  评论(0编辑  收藏  举报