java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 67 path $.data

41 views Asked by At

I have this method and I have a problem with gson in code :

public static CmsUser validateTokenCms(String token, RestTemplate restTemplate, PropertyCommonConfig propertyCommonConfig) {
    TokenRequest tokenRequest = new TokenRequest();
    tokenRequest.setChannel("ORDER_RUNTIME");
    tokenRequest.setTransid(UUID.randomUUID().toString().replace("-", "").substring(0, 10));
    tokenRequest.setToken(token);

    String authString = propertyCommonConfig.getUserNameAuth() + ":" + propertyCommonConfig.getPasswordAuth();
    String encodedAuthString = Base64.getEncoder().encodeToString(authString.getBytes(StandardCharsets.UTF_8));

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + encodedAuthString);
    headers.add("Content-Type", "application/json");

    HttpEntity<TokenRequest> entity = new HttpEntity<>(tokenRequest, headers);

    ResponseEntity<String> responseEntity = null;
    try {
        responseEntity = restTemplate.postForEntity(propertyCommonConfig.getUrlAuthen() + "/customer/checkTokenCms",
                entity, String.class);
    } catch (Exception e) {
        e.printStackTrace();
    }

    log.info("====== responseEntity ======== " + responseEntity);

    Gson gson = new Gson();
    CheckTokenResp response = gson.fromJson(responseEntity.getBody(), CheckTokenResp.class);

    log.info("====== response ======== " + response);

    if (response == null) return null;

    CmsUser cmsUser = response.getData();

    log.info("====== cmsUser ======== " + cmsUser);

    if (cmsUser == null) return null;

    return cmsUser;
}

It shows the following error:

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 67 path $.data

1

There are 1 answers

0
Simon Kocurek On

Print out the body first.

It's likely that it does not start like a JSON object (with {), but instead starts with some other character (possibly a string containing an error message).