解决bug:HttpMessageNotReadableException: JSON parse error: java.lang.Object is not an enum type

àì夳堔傛蜴生んèń 2022-01-27 14:45 1552阅读 0赞

完整的问题如下:

swagger模拟前端请求的json格式为:

  1. {
  2. "enabled": true,
  3. "identification": "string",
  4. "name": "string",
  5. "onwerRoleGroups": [
  6. null
  7. ],
  8. "parentId": 0,
  9. "remark": "string",
  10. "roleCategory": "NORMAL",
  11. "root": true,
  12. "text": "string",
  13. "totalResources": [
  14. {
  15. "enabled": true,
  16. "id": 0,
  17. "operations": [
  18. "RETRIEVE"
  19. ],
  20. "permissionModule": {
  21. "enabled": true,
  22. "identification": "string",
  23. "menu": true,
  24. "name": "string",
  25. "ownerType": "ROLE",
  26. "parentId": 0,
  27. "rank": 0,
  28. "remark": "string",
  29. "root": true,
  30. "uris": [
  31. "string"
  32. ]
  33. },
  34. "rank": 0
  35. }
  36. ]
  37. }

问题出在:

  1. "operations": [
  2. "RETRIEVE"
  3. ]

详细的报错为:

— Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: java.lang.Object is not an enum type; nested exception is com.fasterxml.jackson.databind.JsonMappingException: java.lang.Object is not an enum type (through reference chain: cn.ffcs.videoapp.web.security.permission.entity.PermissionRole[“totalResources”]->java.util.HashSet[0]->cn.ffcs.videoapp.web.security.permission.entity.PermissionResource[“operations”]->java.util.HashSet[0])]
分析的话应该是枚举和javabean识别的问题
先做一个简单的DEMO测试一下
下面的代码是controller

  1. @ApiOperation("呵呵")
  2. @PostMapping("/aaa")
  3. public String addRole1(@RequestBody Person2 person2) {
  4. Set<Sex> sexs = person2.getSexs();
  5. System.out.println(person2.getName());
  6. for (Sex sex : sexs) {
  7. System.out.println(sex.getText());
  8. }
  9. return "ok";
  10. }

下面的封装一个javabean,里面一个字段是Set<枚举>

  1. public class Person2 {
  2. private Set<Sex> sexs=new HashSet<>(0);
  3. private String name;
  4. public Set<Sex> getSexs() {
  5. return sexs;
  6. }
  7. public void setSexs(Set<Sex> sexs) {
  8. this.sexs = sexs;
  9. }
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. }

这个是枚举

  1. public enum Sex {
  2. MAN("男"),WOMAN("女");
  3. private String text;
  4. Sex(String text) {
  5. this.text = text;
  6. }
  7. public String getText() {
  8. return text;
  9. }
  10. public void setText(String text) {
  11. this.text = text;
  12. }
  13. public static void main(String[] args) {
  14. System.out.println(Sex.MAN.getText());
  15. }
  16. }

swagger里面的json是:

  1. {
  2. "name": "string",
  3. "sexs": [
  4. "MAN"
  5. ]
  6. }

结果可以显示
在这里插入图片描述
结论是:普通的枚举类可以通过
“字段”:[“枚举的属性1”,“枚举的属性2”]
入参给controller并进行解析
但是同样的写法.复杂嵌套的java里面套java再套枚举就不行
在网上查询资料:
https://blog.csdn.net/zhangfengaiCQ/article/details/82657926#commentBox
再枚举上加上注解
@JSONType(serializeEnumAsJavaBean = true)
(import com.alibaba.fastjson.annotation.JSONType;)
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
(import com.fasterxml.jackson.annotation.JsonFormat;)
然后再枚举类上加上这两个注解,发现就可以了

找一下为什么

@JSONType(serializeEnumAsJavaBean = true)
作用:配置enum的序列化,可以加在类上,也可以在SerializeConfig 里面配置
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
作用:
(1)@JsonFormat 用来表示json序列化的一种格式或者类型,主要有下面几个常用的属性
shape: 表示序列化后的一种类型

pattern: 表示日期的格式

timezone: 默认是GMT,中国需要GMT+8

locale: 根据位置序列化的一种格式
(2)可用于controller返回给前端responsebody的时候把枚举转换为Object

参考资料

https://segmentfault.com/q/1010000006935643?_ea=1182544
https://blog.csdn.net/u012326462/article/details/83019681
https://blog.csdn.net/zhangfengaiCQ/article/details/82657926#commentBox

思考测试了下.好像有点不对

@JSONType(serializeEnumAsJavaBean = true)好像是没用的
只要@JsonFormat(shape = JsonFormat.Shape.OBJECT)就可以了
本项目之前用的枚举类继承了的类里面已经加了@JsonFormat(shape = JsonFormat.Shape.OBJECT)
本来是可以了.
继承的类上加了

  1. @JsonDeserialize(
  2. using = OptionEnumAwareDeserializer.class
  3. )
  4. @JsonFormat(
  5. shape = Shape.OBJECT
  6. )
  7. public class OptionEnumAwareDeserializer extends JsonDeserializer<OptionEnumAware>

估计改变了json的反序列化的配置规则,所以导致错误
这里序列化指 对象转json字符串
反序列化指string的json转对象
具体还是要去json反序列化配置里面去改

发表评论

表情:
评论列表 (有 0 条评论,1552人围观)

还没有评论,来说两句吧...

相关阅读