简体   繁体   English

Jersey FormDataParam 因 400 Bad Request 失败

[英]Jersey FormDataParam fails with 400 Bad Request

I have a very weird behaviour from Jersey and FormDataParam when trying to upload an image to my service.尝试将图像上传到我的服务时,Jersey 和FormDataParam有一个非常奇怪的行为。 My service runs on Dropwizard.我的服务在 Dropwizard 上运行。

The method that accepts an image (along with metadata object) looks like this:接受图像(连同元数据对象)的方法如下所示:

    @POST
    @Path("/photos")
    @Consumes("multipart/form-data")
    fun upload(
        @FormDataParam("photo") fileInputStream: FileInputStream,
        @FormDataParam("metadata") metadata: PhotoMetadataV1RequestModel
    ) {
        // do something
    }

I've registered MultipartFeature in the Guice module.我已经在 Guice 模块中注册了MultipartFeature

public class JerseyModule extends AbstractModule {

    @Override
    protected void configure() {
        // other stuff

        bind(MultiPartFeature.class).in(Scopes.SINGLETON);
    }

}

I've added a jersey-multipart dependency in the build.gradle .我在build.gradle添加了jersey-multipart依赖build.gradle

    // Jersey
    compile "org.glassfish.jersey.core:jersey-server:$jerseyVersion"
    compile "org.glassfish.jersey.media:jersey-media-multipart:$jerseyVersion"

Now the funny part.现在是有趣的部分。

This actually works if I try to upload a file using an absolute path!如果我尝试使用绝对路径上传文件,这实际上有效 BUT, it does NOT work I try to upload a file using a relative path.但是,它不起作用我尝试使用相对路径上传文件。

More importantly, it also does NOT work when a service is deployed on another machine (not the same the upload image request is coming from).更重要的是,当服务部署在另一台机器上时它也不起作用(上传图像请求来自不同)。 This is important, because I deploy my service to Heroku, and I need to upload images from other places!这很重要,因为我将我的服务部署到 Heroku,并且我需要从其他地方上传图像!

This is what works (absolute path for the photo ):这是有效的photo绝对路径):

curl --location --request POST "http://localhost:8095/rest/v1/self/photos" \
--header "Authorization: Bearer GaKC8xQju5h" \
--form 'photo=/Users/whizzil/Desktop/nova_scripts/create_users/user-lina/photos-webp/photo-profile-1.webp' \
--form 'metadata={"photoType": "PROFILE", "position": 2};type=application/json' 

However, even this does NOT work if the url of the server is not localhost, but eg Heroku server.但是,如果服务器的 url 不是 localhost,而是例如 Heroku 服务器,即使这也不起作用。

This is what does NOT work (relative path for the photo ):这是不起作用的photo相对路径):

curl --location --request POST "http://localhost:8095/rest/v1/self/photos" \
--header "Authorization: Bearer GaKC8xQju5h" \
--form 'photo=@./photo-profile-1.webp' \
--form 'metadata={"photoType": "PROFILE", "position": 2};type=application/json'

The exception that is thrown when it doesn't work:不工作时抛出的异常:

org.glassfish.jersey.internal.inject.ExtractorException: org.glassfish.jersey.internal.inject.ExtractorException: java.io.FileNotFoundException: Invalid file path
org.glassfish.jersey.media.multipart.FormDataParamException: HTTP 400 Bad Request
    at org.glassfish.jersey.media.multipart.internal.FormDataParamValueFactoryProvider$FormDataParamValueFactory.provide(FormDataParamValueFactoryProvider.java:352)
    at org.glassfish.jersey.server.spi.internal.ParamValueFactoryWithSource.provide(ParamValueFactoryWithSource.java:71)
    at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:90)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$AbstractMethodParamInvoker.getParamValues(JavaResourceMethodDispatcherProvider.java:127)

I am lost here.我在这里迷路了。 I tried googling everything, but no success.我尝试谷歌搜索所有内容,但没有成功。 Any help much appreciated!非常感谢任何帮助!

Changing FileInputStream to InputStream solved the issue.FileInputStream更改为InputStream解决了该问题。 I have no idea why, though.不过,我不知道为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM