在Spring Boot中使用Jersey进行文件上传时遇到400错误请求 [英] Using Jersey in Spring Boot for file upload getting 400 Bad Request
问题描述
我尝试了很多使用jersey上传文件的示例.我可以使用@ RequestMapping,ResponseEntity而不是@Path
等使其与纯Spring一起使用.但是我想使用jersey,因为我的所有其他端点都由jersey处理.
I have tried a large number of examples for doing file upload with jersey. I can get it to work with pure Spring using @RequestMapping, ResponseEntity instead of @Path
etc. But I want to use jersey, as all of my other endpoints are handled by jersey.
更新:我觉得我无法传递表单数据,文件或文本.甚至@FormDataParam("directory") String directory
的单个FormDataParam也会发出错误的请求
UPDATE: I feel that I'm unable to pass an form data, file or text. Even a single FormDataParam of @FormDataParam("directory") String directory
gives the bad request
我有以下课程
@Component
@Path("/v1.0")
public class FileOperationsResource {
private ConfigurationReader mConfigReader;
@Autowired
public FileOperationsResource(ConfigurationReader configurationReader) {
mConfigReader = configurationReader;
}
@POST
@Path("/file/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@QueryParam("dir") String directory,
@FormDataParam("file") InputStream file,
@FormDataParam("file") FormDataContentDisposition fileDisposition) {
我已将以下行添加到我的ResourceConfig
I have added the following line to my ResourceConfig
register(MultiPartFeature.class);
我添加了以下Maven依赖项,但未添加版本,因为我的理解是它将自动拉出与我的spring版本兼容的版本,并且我发现较新的版本不再允许我在其中添加注册ResourceConfig
,因为MultiPartFeature
丢失.
I have added the following maven dependency, but have not added a version as my understanding is that it will automatically pull the version that works with my version of spring, and I have found newer versions no longer allow me to add register in ResourceConfig
as MultiPartFeature
is missing.
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
当我拨打以下电话时,我收到了400错误请求.我觉得我一定打错了电话,或者没有将其他东西连上.任何帮助将不胜感激.
When I make the following call, I get a 400 Bad Request. I feel like I must be making the call wrong, or have failed to wire something else in. Any help would be appreciated.
响应:
{
"timestamp": "2018-04-20T15:51:01.790+0000",
"status": 400,
"error": "Bad Request",
"message": "Bad Request",
"path": "/api/v1.0/file/upload"
}
我已经通过Postman进行了呼叫,并使用了表单,并且在接下来的呼叫中使用curl进行了
I've made the call with Postman and using forms, as well as curl with the following call
curl --verbose --form file=@"settings.xml" http://localhost:8080/api/v1.0/file/upload?dir=MyDir
推荐答案
您在application.properties
中将spring.jersey.type
设置为什么?我可以使用Jersey和Boot上传文件,我相信这是您需要的:
What is your spring.jersey.type
set to in application.properties
? I have file upload working with Jersey and Boot, I believe this is what you need:
# JERSEY
spring.jersey.type=servlet
spring.jersey.servlet.load-on-startup=1
出于示例目的,这是我的端点:
For example purposes, here's my endpoint:
@POST
@Path("/file/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileDisposition) {
String fileName = fileDisposition.getFileName();
StringBuilder fileContents = new StringBuilder();
int read = 0;
int totalBytesRead = 0;
byte[] bytes = new byte[1024];
try {
while ((read = fileInputStream.read(bytes)) != -1) {
...save file...
}
} catch (IOException e) {
mLogger.error(e.getMessage(), e);
}
return Response.ok().build();
}
这篇关于在Spring Boot中使用Jersey进行文件上传时遇到400错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!