[英]Java return Inputstream via rest API with html file in inputstream
我目前正在尝试通过我的 API 返回一个输入 stream。输入 stream 包含一个 html 文件,我之前通过 Cdancy Jenkinsclient 通过输入 884041850399 从 Jenkins 获取该文件 884041850399688。 如果我输入Json作为@Produce,那么HTML的内容会附带说明JSON无法解析。 如果我指定另一个 MediyType,则会返回 406。 返回输入流是最好的做法还是我应该先将其转换为输出流?
这是我的代码:
端点
@GET
@Path(API_RESOURCE_IMAGE_REPORT)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Operation(summary = "", description = "")
@APIResponses(
value = {
@APIResponse(
responseCode = "200",
description =
"",
content = @Content(mediaType = MediaType.APPLICATION_JSON)),
@APIResponse(
responseCode = "400",
description = "",
content =
@Content(
mediaType = MediaType.APPLICATION_JSON,
schema = @Schema(implementation = ErrorResponseDO.class))),
})
public Response getReport(@Parameter(
description = "",
required = true)
@PathParam("imageName") final String imageName,
@Parameter(description = "", required = true)
@PathParam("tag") final String tag,
@Parameter(description = "")
@PathParam("type") String type
) throws ApplicationException, IOException {
InputStream report = jenkinsClient.getReport(imageName, tag, type);
return Response.status(HttpURLConnection.HTTP_ACCEPTED).entity(report).build();
}
詹金斯客户:
public InputStream getReport(final String imageName, final String tag, final String type) throws ApplicationException {
try {
final int lastSuccessfulBuildnumber = jenkinsClient.api().jobsApi().jobInfo(imageName, tag).lastSuccessfulBuild().number();
LOG.info("Last successful buildnumber: " + lastSuccessfulBuildnumber);
final InputStream report = jenkinsClient.api().jobsApi().artifact(imageName, tag, lastSuccessfulBuildnumber, Objects.equals(type, "image") ? "trivy_image_report.html" : "trivy_Dockerfile_report.html");
if (report == null) {
throw new NotFoundException();
}
return report;
} catch (Exception e) {
throw new NotFoundException();
}
}
Output:Output 每次都是 406(TEXT_HTML、OCTET_STREAM、TEXT_PLAINE)。 只有 @Produces(MediaType.APPLICATION_JSON) 才能成功使用 html 代码萌芽并显示消息:json 无法解析。
谢谢你的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.