开发者社区> 问答> 正文

JSON数据格式的问题

新项目,前端老哥用的angela框架

后端方法上一定要加个@Responsebody注解才能连通..

加上后,发现我后端postman测试的时候老出错误:

首先出现错误是这样的:

"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",

image.png

使用以下Json格式解决后

image.png

又出现了下面的错误:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Can not deserialize instance of com.****.****.****.**** out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com..****.****.****.**** out of START_ARRAY token
 at [Source: java.io.PushbackInputStream@1a7e14f1; line: 1, column: 1]

之前解决过一次..中间过了好长时间竟然忘了怎么解决的了.. 

好记性不如烂笔头..

解决后还是记下来..

方法1: 给相关的类添加无参构造方法;    不起作用

方法2: 使用ObjectMapper处理复杂类型

String cols="[{'id':'3'},{'id':'3'}]";
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Bean.class);
//Map类型  
mapper.getTypeFactory().constructParametricType(HashMap.class,String.class, Bean.class);
List<Bean> lst =  (List<Bean>)mapper.readValue(cols, javaType); 
 
-------------------------------------------------------------------
String jsonString="[{'id':'1'},{'id':'2'}]";
ObjectMapper mapper = new ObjectMapper();
List<Bean> beanList = mapper.readValue(jsonString, new TypeReference<List<Bean>>() {}); 

上次解决的时候用过, 没有解决我的问题, 证明问题不是出在这..

方法3: 更改入参的json格式

我之前在postman使用的json格式是:

--这个是json的数组格式

[{
    "pageIndex":1,
    "pageSize":6
}]

是不对的.. 参数并不是数组..

改成这样的:

{
    "pageIndex":1,
    "pageSize":6
}

所有问题都解决了...

展开
收起
游客bnlxddh3fwntw 2020-04-23 21:36:53 1000 0
1 条回答
写回答
取消 提交回答
  • 技术架构师 阿里云开发者社区技术专家博主 CSDN签约专栏技术博主 掘金签约技术博主 云安全联盟专家 众多开源代码库Commiter

    看报错,必然是传输问题,所以找到源头,解决请求响应格式就好了

    2020-04-24 22:26:02
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载