为什么我得到XML响应而不是JSON?

时间:2019-05-20 09:10:11

标签: java php json spring

我需要生成一个报告,以便使用Java Spring的RestTemplate与php服务进行通信。我与php服务通讯时的代码是:-

RestTemplate template = new RestTemplate();
ResponseEntity<String> response = null;

Gson gson = new Gson();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
String reqBodyJSON = gson.toJson(reportPayload);
String Url = HmCommonProperty.getProperty("report_generate_url");
HttpEntity<String> entity = new HttpEntity<String>(reqBodyJSON, headers);

try {
    response = template.postForEntity(Url, entity, String.class);
}
catch(Exception ex) {
    log.error("Exception occured with cause "+ex.getMessage());
}

在成功生成后,应该返回这样的json:-

{
    "status": 200,
    "status_message": "SUCCESS",
    "data": "FILE_CREATED"
}

如果我直接从Postman击中php服务,我将得到相同的成功响应。但是从我的RestTemplate中,我得到的响应根本不是json。这样的XML很奇怪:-

<200,<br />
<b>Notice</b>:  Undefined index: diagnostic_report in <b>C:\xampp\htdocs\worksheet\mind_stretches_report\benchmark_reports.php</b> on line <b>198</b><br />
<br />
<b>Warning</b>:  Invalid argument supplied for foreach() in <b>C:\xampp\htdocs\worksheet\mind_stretches_report\benchmark_reports.php</b> on line <b>198</b><br />
{"status":200,"status_message":"SUCCESS","data":"FILE_CREATED"}<br />
<b>Warning</b>:  filesize(): stat failed for ./logger.log in <b>C:\xampp\htdocs\worksheet\mind_stretches_report\log4php\appenders\LoggerAppenderRollingFile.php</b> on line <b>223</b><br />
,{Date=[Mon, 20 May 2019 09:07:07 GMT], Server=[Apache/2.4.18 (Win32) OpenSSL/1.0.2e PHP/7.0.4], X-Powered-By=[PHP/7.0.4], Content-Length=[591], Keep-Alive=[timeout=5, max=100], Connection=[Keep-Alive], Content-Type=[application/json]}>

如何获取我想要的json而不是此xml响应?

1 个答案:

答案 0 :(得分:-1)

将您通过Postman发送的请求与您的PHP发送的请求进行比较。

您是否发送相同的请求类型?相同的变量?

PHP脚本期望设置“ diagnostic_report”,但没有设置。