JSON parse error: Cannot deserialize instance of `java.util.ArrayList<..> out of START_OBJECT token;

JSON parse error: Cannot deserialize instance of java.util.ArrayList<com.sangfor.ngsoc.knowledge.to.UserEntity> out of START_OBJECT token;

1. 问题复现

① 首先将knowledge和auth服务都配置进eureka:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

② auth服务的项目路径为:

server:
  port: 8081
  servlet.context-path: /ngso/AUTH/

③ 远程auth服务的接口:

@Api(tags = "用户表")
@Slf4j
@Validated
@RestController
@ResponseResult
@RequestMapping("/api/v2")
public class UserController {

    @Autowired
    private IUserService userService;

    @PostMapping("/users/inner/exact")
    public PageData<UserEntity> exactPageQueryUserList(@Validated @RequestBody UserQo userQo,
                                                       @RequestParam(SessionKey.DOMAIN_ID) String domainId) {
        userQo.setDomainId(domainId);
        return userService.exactPageQueryUserList(userQo);
    }
}

④ knowledge服务的feign接口,调用远程auth服务的接口:

@FeignClient(name = "AUTH")
public interface AuthFeignService {
    @PostMapping("/ngso/AUTH/api/v2/users/inner/exact")
    PageData<UserResp> exactPageQueryUserList(
            @RequestBody UserReqDto userReqDto,
            @RequestParam(SessionKey.DOMAIN_ID) String domainId);
}

⑤ 在knowledge服务中调用feign接口:

@Override
public String addDoc() {
    List<String> userIds = new ArrayList<>();
     UserInfo userInfo = UserInfoShareHolder.getUserInfo();
    userIds.add(userInfo.getId());
    UserReqDto userReqDto = new UserReqDto(userIds);
    userReqDto.setPageNum(1);
    userReqDto.setPageSize(1);
    PageData<UserResp> pageDataApiResponse = authFeignService
        .exactPageQueryUserList(userReqDto, UserInfoShareHolder.getUserInfo().getDomainId());
}

结果报错:

JSON parse error: Cannot deserialize instance of `java.util.ArrayList<com.sangfor.ngsoc.knowledge.to.UserEntity>` out of START_OBJECT token; 

2. 解决方法

① 首先直接调用auth服务的接口,不使用远程服务:localhost:8081/ngso/AUTH/api/v2/users/inner/exact

比实际的响应数据多了code,message这些信息,说明又对响应数据进行了封装

{
    "code": 0,
    "message": "成功",
    "data": {
        "pageNum": 1,
        "pageSize": 1,
        "totalCount": 1,
        "data": [
            {
                "id": "fx-user-fec844b9e0dc0d80000004",
                "name": "sysadmin",
                "phone": "",
                "email": "",
                "description": "内置用户:默认平台系统管理员账户",
                "domainId": "fx-domain-fec833bfa0dbcc40000001",
                "projectId": " ",
                "enabled": 1,
                "createTime": "2021-10-18 15:41:17",
                "updateTime": "2021-10-21 16:35:11",
                "changeTime": "2021-10-18 16:44:37",
                "loginTime": "2021-10-21 16:35:11",
                "options": 0,
                "builtIn": 1
            }
        ]
    }
}

② 查看代码加上一层封装即可:

@FeignClient(name = "AUTH" )
public interface AuthFeignService {
   @PostMapping("/ngso/AUTH/api/v2/users/inner/exact")
    ApiResponse<PageData<UserResp>> exactPageQueryUserList(
            @RequestBody UserReqDto userReqDto,
            @RequestParam(SessionKey.DOMAIN_ID) String domainId);
}
上一篇:Java --- 多线程 创建线程的方式四: 使用线程池


下一篇:Java代码生成器更新:添加多数据源模式支持