我把List<String>作为GET请求的返回数据,报错[Exception] com.alibaba.fastjson.JSONArray cannot be cast to com.alibaba.fastjson.JSONObject java.lang.ClassCastException: com.alibaba.fastjson.JSONArray cannot be cast to com.alibaba.fastjson.JSONObject
时间: 2023-10-14 15:46:31 浏览: 436
这个错误是由于你的代码中存在类型转换错误导致的。
根据你提供的错误信息,可以看到将`com.alibaba.fastjson.JSONArray`转换为`com.alibaba.fastjson.JSONObject`时出现了`ClassCastException`。这意味着你的返回数据实际上是一个JSON数组(`JSONArray`),而你尝试将其强制转换为JSON对象(`JSONObject`)时引发了异常。
通常情况下,当使用`List<String>`作为返回类型时,Spring会自动将其转换为JSON数组,并将响应头的`Content-Type`设置为`application/json`,以表明返回的内容是JSON格式的。因此,客户端应该能够正确解析这个JSON数组。
你可以尝试使用以下方式修改你的代码,以便正确返回JSON数组:
```java
@GetMapping("/investor/share/get/certino")
public ResponseEntity<List<String>> queryInvestorCertiNo(@RequestParam("custName") String custName) {
List<String> certiNoList = taFormService.queryCertiNo(custName);
return ResponseEntity.ok(certiNoList);
}
```
在上面的示例中,我们使用了`ResponseEntity<List<String>>`作为返回类型,这样可以更好地表示返回的是一个包含`List<String>`的响应实体。
另外,请确保客户端能够正确处理接收到的JSON数组数据。使用类似于`com.alibaba.fastjson.JSONArray.parseArray()`或其他JSON解析库来解析返回的JSON数组。
如果你还有其他问题,请随时提问。
阅读全文