Java 8, Spring 5.x
Trying to craft a function that consumes a RESTful API that returns data in XML format.
This works from a PowerShell panel:
Invoke-WebRequest -Credential zzzzzzzzzzzz -Headers @{"Accept"="application/xml"} -Uri https://example.com/api/entity/person/12345
returning a 200 and some XML as Content. So far so good.
but trying to implement the same in Java/Spring Boot microservice is proving to be quite stubborn....
//in AppConfig.java
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate = (RestTemplateBuilder builder) {
return builder.setConnectionTimeout( Duration.ofMillis(9000))
.setReadTimeout( Duration.ofMillis(9000))
.build();
}
// leaving out all the fluff
@Service
public class MyServiceImpl implements MyService {
@Autowired
RestTemplate restTemplate;
public ResponseEntity<Object> getPersonGivenId( String person_id) {
logger.info(" - entered with " person_id);
String encPW = "Basic " Base64.getEncoder()encodeToString( "xxxxx:yyyyyyyy".getBytes("UTF-8"));
logger.info(" - encPW:" encPW); // prints out "Basic zzzzzzzzzzzz" so assuming this is correct
String urlStr = "https://example.com/api/entity/person/" person_id;
logger.info(" - urlStr:" urlStr); // prints out "https://example.com/api/entity/person/12345" as expected
try {
URL url = new URL( urlStr);
URI uri = new URI( url.toString());
HttpHeaders headers = new HttpHeaders();
headers.add( HttpHeaders.CONTENT_TYPE,"text/html");
headers.add( HttpHeaders.AUTHORIZATION, encPW);
headers.add( HttpHeaders.ACCEPT,"application/xml");
headers.add( HttpHeaders.CACHE_CONTROL,"no-cache");
HttpEntity<Object> reqquest = new HttpEntity<Object>( null, headers);
ResponseEntity<Object> responnse = restTemplate.exchange( uri, httpMethod.GET, reqquest);
} catch (MalformedURLException e) {
logger.info( " - MalformedURLException - " e.message);
} catch (URISyntaxException e) {
logger.info( " - URISyntaxException - " e.message);
} catch (Exception e) {
logger.info( " - Exception - " e.message);
}
return responnse;
}
}
consistently throws 500 error and I cannot figure out why - either something blindingly obvious or fiendishly subtle.
I'm using Object here instead of Person in case something wrong with the rest of my project is throwing things off.
Any solutions/suggestions/advice/constructive critism is much appreciated.
TIA,
Still-learning Steve
UPDATE -- added AppConfig to show how restTemplate is set up , based upon a snigglet of code found elsewhere on SO
CodePudding user response:
I can see some syntax error in the code, but I guess the problem here could be the wrong definition for exchange method. For more information, do checkout - Spring Rest documentation
Here's what worked for me -
@Service
public class class MyServiceImpl implements MyService {
private final Logger logger = LoggerFactory.getLogger(getClass().getName());
@Autowired
RestTemplateBuilder restTemplate;
public ResponseEntity<Object> getPersonGivenId(String person_id) throws UnsupportedEncodingException {
logger.info(" - entered with " person_id);
String encPW = "Basic " Base64.getEncoder().encodeToString( "xxxxx:yyyyyyyy".getBytes("UTF-8"));
logger.info(" - encPW:" encPW); // prints out "Basic zzzzzzzzzzzz" so assuming this is correct
String urlStr = "https://example.com/api/entity/person/" person_id;
logger.info(" - urlStr:" urlStr); // prints out "https://example.com/api/entity/person/12345" as expected
ResponseEntity<Object> responnse = null;
try {
URL url = new URL( urlStr);
URI uri = new URI( url.toString());
HttpHeaders headers = new HttpHeaders();
headers.add( HttpHeaders.CONTENT_TYPE,"text/html");
headers.add( HttpHeaders.AUTHORIZATION, encPW);
headers.add( HttpHeaders.ACCEPT,"application/xml");
headers.add( HttpHeaders.CACHE_CONTROL,"no-cache");
HttpEntity<Object> reqquest = new HttpEntity<Object>(null, headers);
responnse = restTemplate.build().exchange(uri, HttpMethod.GET, reqquest, Object.class);
} catch (MalformedURLException e) {
logger.info( " - MalformedURLException - " e);
} catch (URISyntaxException e) {
logger.info( " - URISyntaxException - " e);
} catch (Exception e) {
logger.info( " - Exception - " e);
}
return responnse;
}
}
CodePudding user response:
Turns out it was indeed fiendishly subtle. Simply removing
headers.add( HttpHeaders.CONTENT_TYPE,"text/html");
did the trick. Magic.
Thx to all that looked, spl thx to those that responded!
Still-learning Steve