An official website of the United States government
Accessing the BLS Public Data API with JavaOn This Page:API Version 2.0 Java Sample CodeSpring Framework:You can access the API using SpringSource's RestTemplate exchange method. The exchange method has four parameters:
For example:public ResponseEntity exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, Class responseType) throws RestClientException
The following example assumes the requestType class is APIRequest and the responseType class is APIResponse.class: ResponseEntity responseEntity = restTemplate.exchange(apiURL, HttpMethod.POST, HttpEntity httpEntity, APIResponse[].class); APIResponse[] = responseEntity.getBody() The getBody() method returns the response mapped to the responseType class (i.e. the APIResponse.class). Once the response from API service has been mapped to a response type object, regular Java operation can be performed on the POJO response object. The requestEntity and the responseType can use the Jackson library to bind request and response objects to the JSON request and response. Calling the RestTemplate's exchange method returns a specific ResponseEntity object.
Apache HTTP Commons Client:The HttpPost\HttpGet class can be used to connect to the BLS Public Data API via Apache HTTP Commons Client, as shown in the following example. Remember to include JSON as the ContentType. HttpPost httpPost = new HttpPost("https://api.bls.gov/publicAPI/v2/timeseries/data/"); StringEntity input = new StringEntity("{\"seriesid\":[\"LAUCN040010000000005\", \"LAUCN040010000000006\"]}"); input.setContentType("application/json"); postRequest.setEntity(input); HttpResponse response = httpClient.execute(postRequest); variableFoo = response.getEntity().getContent()
API Version 1.0 Java Sample CodeSpring Framework:You can access the API using SpringSource's RestTemplate exchange method. The exchange method has four parameters:
For example:public ResponseEntity exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, Class responseType) throws RestClientException
The following example assumes the requestType class is APIRequest and the responseType class is APIResponse.class: ResponseEntity responseEntity = restTemplate.exchange(apiURL, HttpMethod.POST, HttpEntity httpEntity, APIResponse[].class); APIResponse[] = responseEntity.getBody() The getBody() method returns the response mapped to the responseType class (i.e. the APIResponse.class). Once the response from API service has been mapped to a response type object, regular Java operation can be performed on the POJO response object. The requestEntity and the responseType can use the Jackson library to bind request and response objects to the JSON request and response. Calling the RestTemplate's exchange method returns a specific ResponseEntity object.
Apache HTTP Commons Client:The HttpPost\HttpGet class can be used to connect to the BLS Public Data API via Apache HTTP Commons Client, as shown in the following example. Remember to include JSON as the ContentType. HttpPost httpPost = new HttpPost("https://api.bls.gov/publicAPI/v1/timeseries/data/"); StringEntity input = new StringEntity("{\"seriesid\":[\"LAUCN040010000000005\", \"LAUCN040010000000006\"]}"); input.setContentType("application/json"); postRequest.setEntity(input); HttpResponse response = httpClient.execute(postRequest); variableFoo = response.getEntity().getContent()
Last Modified Date: October 16, 2014 |