尝试使分段文件上载以在Grails 2.5.4资源中工作。
我真的很想使用Spring的现成的多部分功能,但是我找不到适合我的特殊情况的示例。
我正在从邮递员那里调用API。在form-data
字段中带有file
和单个PDF文件的正文。没有其他标题!
@POST
@Path("/pdf")
Response createPdf(MultipartFile file)
在Postman中退回415 Unsuported Media Type
,并在服务器上将其退回:
[...] ERROR container.ContainerRequest - A message body reader for Java class org.springframework.web.multipart.MultipartHttpServletRequest [...]
The registered message body readers compatible with the MIME media type are:
*/* ->
com.sun.jersey.core.impl.provider.entity.FormProvider
com.sun.jersey.core.impl.provider.entity.StringProvider
com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
com.sun.jersey.core.impl.provider.entity.FileProvider
com.sun.jersey.core.impl.provider.entity.InputStreamProvider
com.sun.jersey.core.impl.provider.entity.DataSourceProvider
com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.ReaderProvider
com.sun.jersey.core.impl.provider.entity.DocumentProvider
com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
com.sun.jersey.core.impl.provider.entity.EntityHolderReader
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
添加@Consumes(MediaType.MULTIPART_FORM_DATA)
无济于事。
我尝试了其他参数,例如MultipartHttpServletRequest
,但仍然没有消息正文阅读器。
@POST
@Path("/pdf")
@Consumes(MediaType.MULTIPART_FORM_DATA)
Response createPdf(@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail)
返回500错误,此错误在服务器上:
SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.carfax.VehicleReceiptPhotoResource.createPdf(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at parameter at index 0
SEVERE: Missing dependency for method public javax.ws.rs.core.Response createPdf(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at parameter at index 1
SEVERE: Method, public javax.ws.rs.core.Response createPdf(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), annotated with POST of resource, class com.carfax.VehicleReceiptPhotoResource, is not recognized as valid resource method.
具有这些依赖性
compile group: 'com.sun.jersey', name: 'jersey-bundle', version: '1.19.4'
compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19.4'
compile "javax.ws.rs:javax.ws.rs-api:2.1@jar"
compile ('org.glassfish.jersey.media:jersey-media-multipart:2.27') {
exclude group:'javax.ws.rs'
}
compile group: 'org.jvnet.mimepull', name: 'mimepull', version: '1.9.11'
@POST
@Path("/pdf")
Response createPdf(@FormDataParam("file") InputStream fileInputStream)
与第二次尝试具有相同的球衣依赖关系,此方法有效,但对我来说还不够好。我想一次上传多个文件,并在侧面获得一些额外的json信息。