500 内部服务器错误:[{“错误":[“解析值时遇到意外字符:%.路径'',第 0 行,位置 0."]}] [英] 500 Internal Server Error: [{"errors":["Unexpected character encountered while parsing value: %. Path '', line 0, position 0."]}]

查看:32
本文介绍了500 内部服务器错误:[{“错误":[“解析值时遇到意外字符:%.路径'',第 0 行,位置 0."]}]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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);
        

这篇关于500 内部服务器错误:[{“错误":[“解析值时遇到意外字符:%.路径'',第 0 行,位置 0."]}]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆