jsreport-api用java从响应中保存pdf

s2j5cfk0  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(345)

我需要保存邮政服务的pdf,但我总是收到错误:
“提取类型[class java.lang.object]和内容类型[application/pdf]的响应时出错;嵌套异常为org.springframework.http.converter.httpMessageToradableException:json分析错误:意外字符(“%”(代码37)):应为有效值(json字符串、数字、数组、对象或标记“null”、“true”或“false”);嵌套异常为com.fasterxml.jackson.core.jsonparseexception:意外字符(“%”(代码37)):应为有效值(json字符串、数字、数组、对象或标记“null”、“true”或“false”)
我使用的是springboot和rest模板
例子:

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);
    }
}
t5zmwmid

t5zmwmid1#

在这种情况下,curl对调试非常有用。
如果我使用您的属性、auth、json负载和api端点,并使用curl发布请求 curl -i --user test_pdf:test_pdf -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"template":{"shortid":"JCdUaT_"},"data":null}' https://evx.jsreportonline.net/api/report 我有以下几种React

HTTP/2 200 
date: Fri, 24 Jan 2020 19:45:07 GMT
content-type: application/pdf
server: nginx/1.17.6
x-powered-by: Express
access-control-allow-origin: *
access-control-expose-headers: *
set-cookie: render-complete=true; Path=/
set-cookie:  
content-disposition: inline;filename=invoice-main.pdf
jo-credits-spent: 1.12

Warning: Binary output can mess up your terminal. Use "--output -" to tell 
Warning: curl to output it to your terminal anyway, or consider "--output 
Warning: <FILE>" to save to a file.

这是告诉我,我得到二进制数据作为回应。在这种情况下,我需要使用不同的转换器。试试这里显示的“bytearrayhtpmessageconverter”

huwehgph

huwehgph2#

我解决了。同样,非常感谢那些作出回应的人。

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() {

Json reporte = new Json("{\"template\":{\"shortid\":\"ytyg1Ho\"},\"data\":null}");

List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

/* MENSAGE CONVERTER */
messageConverters.add(new ByteArrayHttpMessageConverter());
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
messageConverters.add(converter);

RestTemplate restTemplate = new RestTemplate(messageConverters);

 // REQUEST
HttpEntity<Object> entity = new HttpEntity<>(reporte, this.getHeaders());

   /* RESPONSE */
try {
    byte[] response = restTemplate.postForObject(base_url, entity, byte[].class);
    System.out.println("Response "+new String(response));
}catch (Exception e) {
    System.out.println(e.getMessage() +" "+ e);
}

}

相关问题