500 内部服务器错误:[{“错误":[“解析值时遇到意外字符:%.路径'',第 0 行,位置 0."]}]错误、路径、字符、意外

2023-09-07 14:15:19 作者:醉魂愁梦相伴

我正在使用 RestTemplate 调用 AutoTask API.但是,我收到一条错误消息: 500 Internal Server Error: [{"errors":[""Unexpected character met while paring value: %.路径 '',第 0 行,位置 0."]}]

I am using RestTemplate to call an AutoTask API. However I am getting an error that says: 500 Internal Server Error: [{"errors":["Unexpected character encountered while parsing value: %. Path '', line 0, position 0."]}]

API 调用的 Swagger UI 截图(成功)

控制台输出(错误)

代码:

@GetMapping("/all-clients")
private String getAllClients() {
    
    String COMPANIES_API_URL_Prefix = "https://webservices14.autotask.net/ATServicesRest/V1.0/Companies/query?search=";
    String COMPANIES_API_URL_Suffix = "{"filter":[{"op":"in","field":"CompanyType","value":[1]}]}";
    
     try {
            HttpHeaders headers = new HttpHeaders();
            headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
            headers.set("ApiIntegrationCode", "HUCXSL....."); //values partially hidden as it is sensitive information
            headers.set("UserName", "fdfsf...."); //values partially hidden as it is sensitive information
            headers.set("Secret", "yR*42......"); //values partially hidden as it is sensitive information

            HttpEntity entity = new HttpEntity(headers);
           
            RestTemplate restTemplate = new RestTemplate();
            
            String url = COMPANIES_API_URL_Prefix+URLEncoder.encode(COMPANIES_API_URL_Suffix);
            
            
            System.out.println(entity);
            
            System.out.println(url);
            
            ResponseEntity<Company[]> response = restTemplate.exchange(url, HttpMethod.GET, entity, Company[].class);
            
            System.out.println("Result - status ("+ response.getStatusCode() + ") has body: " + response.hasBody());
        }
     
        catch (Exception exception) {
            
            System.out.println("** Exception: "+ exception.getMessage());
        }
     
     return "all_clients";
}

推荐答案

您正在对 JSON 有效负载进行编码以解决问题或尝试如下.在 httpEntity 中设置 JSON 负载.

You are doing encoding the JSON payload also that casing the problem or try as below. Set the JSON payload in httpEntity.

HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.ACCEPT, 
MediaType.APPLICATION_JSON_VALUE);
        headers.set("ApiIntegrationCode", "HUCXSL....."); //values 
partially hidden as it is sensitive information
        headers.set("UserName", "fdfsf...."); //values partially 
hidden as it is sensitive information
        headers.set("Secret", "yR*42......"); //values partially hidden as it is sensitive information

        HttpEntity entity = new HttpEntity(headers, 
COMPANIES_API_URL_Suffix);;
       
        RestTemplate restTemplate = new RestTemplate();
        
    String url=OMPANIES_API_URL_Prefix;

        ResponseEntity<Company[]> response = 
 restTemplate.exchange(url, HttpMethod.GET, entity, 
Company[].class);