Bug记录: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-06-07 21:15  爱吃麦辣鸡翅  阅读(144)  评论(0编辑  收藏  举报