带有@products MediaType.APPLICATION_OCTET_流的Java Rest服务返回json而不是pdf文件
我有一个JavaREST服务,它应该返回一个PDF 这是我的代码:带有@products MediaType.APPLICATION_OCTET_流的Java Rest服务返回json而不是pdf文件,java,rest,Java,Rest,我有一个JavaREST服务,它应该返回一个PDF 这是我的代码: @RequestMapping(value = "/pdf", method = RequestMethod.GET) @Produces(MediaType.APPLICATION_OCTET_STREAM) public javax.ws.rs.core.Response getPdf() throws Exception { File file = new File("xxx"
@RequestMapping(value = "/pdf", method = RequestMethod.GET)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public javax.ws.rs.core.Response getPdf() throws Exception {
File file = new File("xxx");
javax.ws.rs.core.Response.ResponseBuilder response = javax.ws.rs.core.Response.ok((Object) file);
response.header("Content-Disposition", "attachment;filename=" + file);
return response.build();
}
但结果总是一个json,而不是一个要下载或打开的文件。为什么?
{
"context": {
"headers": {
"Content-Type": [
{
"type": "application",
"subtype": "octet-stream",
"parameters": {},
"wildcardType": false,
"wildcardSubtype": false
}
],
"Content-Disposition": [
"attachment; filename=\"12-20200313.pdf\""
]
},
"configuration": null,
"entity": "e:\\content\\12-20200313.pdf",
"entityType": "java.io.File",
"entityAnnotations": [],
"entityStream": {
"closed": false,
"committed": false
},
"length": -1,
"location": null,
"language": null,
"lastModified": null,
"date": null,
"acceptableMediaTypes": [
{
"type": "*",
"subtype": "*",
"parameters": {},
"quality": 1000,
"wildcardType": true,
"wildcardSubtype": true
}
],
"acceptableLanguages": [
"*"
],
"committed": false,
"lengthLong": -1,
"mediaType": {
"type": "application",
"subtype": "octet-stream",
"parameters": {},
"wildcardType": false,
"wildcardSubtype": false
},
"allowedMethods": [],
"requestCookies": {},
"entityClass": "java.io.File",
"responseCookies": {},
"entityTag": null,
"links": [],
"stringHeaders": {
"Content-Type": [
"application/octet-stream"
],
"Content-Disposition": [
"attachment; filename=\"12-20200313.pdf\""
]
}
},
"status": 200,
"length": -1,
"location": null,
"language": null,
"lastModified": null,
"date": null,
"entity": "e:\\content\\12-20200313.pdf",
"cookies": {},
"mediaType": {
"type": "application",
"subtype": "octet-stream",
"parameters": {},
"wildcardType": false,
"wildcardSubtype": false
},
"allowedMethods": [],
"metadata": {
"Content-Type": [
{
"type": "application",
"subtype": "octet-stream",
"parameters": {},
"wildcardType": false,
"wildcardSubtype": false
}
],
"Content-Disposition": [
"attachment; filename=\"12-20200313.pdf\""
]
},
"entityTag": null,
"links": [],
"stringHeaders": {
"Content-Type": [
"application/octet-stream"
],
"Content-Disposition": [
"attachment; filename=\"12-20200313.pdf\""
]
},
"statusInfo": "OK",
"headers": {
"Content-Type": [
{
"type": "application",
"subtype": "octet-stream",
"parameters": {},
"wildcardType": false,
"wildcardSubtype": false
}
],
"Content-Disposition": [
"attachment; filename=\"12-20200313.pdf\""
]
}
}
应该从经典ASP应用程序调用此服务,然后将pdf保存到磁盘。
所以我需要的是返回可以保存的东西的服务。我还尝试返回表示pdf文件的base64字符串,然后在客户端对其进行解码并保存到磁盘。这是可行的,但这里有两个问题。首先,我在编码的pdf字符串中看到了许多\r\n内容,在尝试在客户端解码之前需要删除这些内容。第二,在客户端上创建pdf文件需要几分钟的时间
首选的解决方案是下载它,但它总是返回json
谢谢javax.ws.rs.core.Response不是Spring类型。使用文件系统资源。请参阅javax.ws.rs.core.Response不是Spring类型。使用文件系统资源。看见