IllegalArgumentException的REST调用问题

时间:2018-07-02 07:06:49

标签: java rest api spring-boot

这是我要尝试进行简单REST调用的函数。

private String getWorkdayDataArray() throws IllegalArgumentException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
        String url = getWorkers();
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpResponse httpResponse = httpClient.execute(new HttpGet(url));
        return EntityUtils.toString(httpResponse.getEntity());
    }

private String getWorkers() throws IllegalArgumentException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException
    {

        RestTemplate restTemplate = getRestTemplate();
        HttpEntity<String> entity = getEntityForWorkday();
        String resp = null;
        String url = String.format("https://XXXXXXXXXXX.com/dorqers/v5?key=%s",AUTH_KEY);
        try {
            ResponseEntity<String> respEntity = restTemplate.exchange(url,HttpMethod.GET,entity,String.class);
            resp = respEntity.getBody();
            resp.replaceAll(" ", "");
        }
        catch (Exception e){
            e.printStackTrace();
        }
return resp;
    }

public HttpEntity<String> getEntityForWorkday()
    {
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization",AUTHENTICATION_TOKEN);
        headers.set("Content-Type","application/json");
        return new HttpEntity<>("parameters",headers);
    }

    public RestTemplate getRestTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException
    {
        //Disable SSL
        org.apache.http.conn.ssl.TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
        SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
                .loadTrustMaterial(null, acceptingTrustStrategy)
                .build();
        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(csf)
                .build();
        HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }

get呼叫在邮递员中工作正常。当我尝试通过api执行此操作时,出现以下错误:

java.lang.IllegalArgumentException: Illegal character in scheme name at index 0: {"total_workers":2228,"workers":
[{"worker":{"update_identifier":"asssy25-JUN-18 06.24.52.429287 AM -05:00","identity":
{"first_name":"damam",
"middle_name":"A",
"last_name":"Craft",
"preferred_first_name":"damam",
"preferred_last_name":"Craft",
"lan_id":"T003","worker_type":"Team Member",
"active_directory_worker_id":"01039932","is_manager":"N"
},"location":{
"location_type":"OFFICE","work_state":"CA"
},"email":
{"work_email":"damam.craft@tt.com",
"supervisor_work_email":"barra.kosi@tt.com"
},"job":
{"job_description":"Sr Coordinator Service Center - US",
"job_entry_date":"2011-04-10",
"pay_type_code":"Hourly",
"position_entry_date":"2011-04-10",
"active_directory_supervisor_id":"hhhgffssaa",
"standard_hours":"40.00","weekly_work_hours":"40.00",
"time_type":"Full time"
},"employee":
{"employee_status_description":"Active"},
"employee_action":{"hire_date":"1978-04-04","rehire_date":"1978-04-04"}}},

它执行并打印从GET调用返回的整个JSON对象。这是错误日志的其余部分:

 at java.net.URI.create(URI.java:852)
        at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:66)
        at application.Collector.WorkdayCollector.getWorkdayDataArray(WorkdayCollector.java:65)
        at application.Collector.WorkdayCollector.getWorkdayData(WorkdayCollector.java:43)
        at application.Application.lambda$run$0(Application.java:26)
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732)
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)
        at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
        at application.Application.main(Application.java:13)
    Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 0: <Prints entire JSON Again>

at java.net.URI$Parser.fail(URI.java:2848)
    at java.net.URI$Parser.checkChars(URI.java:3021)
    at java.net.URI$Parser.checkChar(URI.java:3031)
    at java.net.URI$Parser.parse(URI.java:3047)
    at java.net.URI.<init>(URI.java:588)
    at java.net.URI.create(URI.java:850)
    ... 11 more

我已经做过一些研究,并且URL中有空格时很可能会发生错误。我已经仔细检查过,并且我的网址中似乎没有空格。最奇怪的是,日志正在打印整个JSON,但我没有在任何地方将其显式打印在代码中。

在此问题上的任何帮助都将受到高度赞赏。

0 个答案:

没有答案