我正在使用java应用程序中的两个应用程序我想使用httppost将报告发送到spring应用程序,我创建了端点,在尝试发送报告之后,我得到了关于成功身份验证的响应,另外我得到了异常:
JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 153] (through reference chain: pl.mr.form.ReportDataForm["reportData"])]
我的报告看起来如何:
public class FileResult {
private String filePath;
private String diskName;
private String status;
另一个类具有fileresult对象列表:
public class FileSystemResult extends ReportData {
private List<FileResult> fileResults;
}
reportdata类是空类,只是为了添加posibility extend others类而创建的,我的报表类看起来:
public class Report implements Serializable {
private Type type; // enum
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_TYPE)
private ZonedDateTime scanDate;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_TYPE)
private ZonedDateTime reportDate;
private ReportData reportData;
private String serailId;
这是我发报告的班级:
public void sendReport(final Report report) {
if (report == null) {
log.error("Can't send null");
}
try (CloseableHttpClient client = HttpClients.createDefault()) {
String token = getTokenFromProperties();
if (token != null) {
log.info("Token is equals = " + token);
} else {
log.error("Token not found");
}
HttpPost httpPost = new HttpPost("http://localhost:8080/marcin/sendReport");
String json = convertReportToJson(report);
log.info(json);
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Authorization", "Bearer " + token);
httpPost.setHeader("Content-type", "application/json;charset=UTF-8");
CloseableHttpResponse response = client.execute(httpPost);
log.info(response.toString());
} catch (IOException e) {
e.printStackTrace();
log.error("" + e.getMessage());
}
}
private String convertReportToJson(Report report) {
ObjectMapper mapper = new ObjectMapper();
try {
mapper.registerModule(new JavaTimeModule());
return mapper.writeValueAsString(report);
} catch (JsonProcessingException e) {
e.printStackTrace();
return null;
}
}
我要发这个报告的主要课程是:
public static void main(String[] args) {
FileSystemResult fileSystemResult = new FileSystemScanResult();
fileSystemResult.setFileResults(List.of(new FileResult("ABC", "mroma", "OK"),
new FileScanResult("POL", "mroma", "ERROR")));
Report newReport = new Report(Report.Type.FILE_SCAN, ZonedDateTime.now(ZoneId.of("UTC")), ZonedDateTime.now(ZoneId.systemDefault()), fileSystemResult);
ReportService rs = new ReportService();
rs.sendReport(newReport);
}
另外,当我从report类中删除reportdata时,一切正常
暂无答案!
目前还没有任何答案,快来回答吧!