I need to save the PDF of the POST service, but I always receive the error:
"Error while extracting response for type [class java.lang.Object] and content type [application/pdf]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')"
I am using Spring Boot and RestTemplate
Example:
public static final String base_url = "https://evx.jsreportonline.net/api/report";
static {
HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> hostname.equals("evx.jsreportonline.net"));
}
private HttpHeaders getHeaders() {
if (null == headers) {
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Arrays.asList(MediaType.ALL));
headers.add("Authorization", "Basic " + Base64.getEncoder().encodeToString("test_pdf:test_pdf".getBytes()));
}
return headers;
}
public void getRepo() {
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
RestTemplate restTemplate = new RestTemplate();
Json jsonObj = new Json("{\"template\":{\"shortid\":\"JCdUaT_\"},\"data\":null}");
// REQUEST
HttpEntity<Object> entity = new HttpEntity<>(jsonObj, this.getHeaders());
/* MENSAGE CONVERTER */
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
messageConverters.add(converter);
restTemplate.setMessageConverters(messageConverters);
/* RESPONSE */
try {
Object result = restTemplate.postForObject(base_url, entity, Object.class);
byte[] resultByteArr = (byte[]) result;
} catch (Exception e) {
System.out.println(e.getMessage() + " " + e);
}
}