0

我正在使用 RestTemplate 调用 AutoTask API。但是我收到一条错误消息: 500 Internal Server Error: [{"errors":["Unexpected character met 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";
}
4

1 回答 1

0

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

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);
        
于 2021-07-06T16:37:05.710 回答