REST- Jersey - Exception obtaining parameters

2019-08-22 02:29发布

Here is my ajax call:

uploadExcel : function(jsonData,success, error) {
    var url = "/TestProject/test/Uploader;
    $.ajaxFileUpload({ 
        url : url,
        secureuri : false,
        fileElementId : 'FileUpload',
        contentType : 'multipart/form-data',
        dataType : 'jsonString',
        processData : false,
        type : 'POST',
        data: jsonData,
        success : success,
        error : error
    });
}

Java Method signature:

@Path("/Uploader") 
@POST
@Consumes('multipart/form-data')
public String validateAndUpload(@FormDataParam("FileUpload") byte[] inputByteArray, 
                                @Context HttpServletRequest request,
                                @FormParam("jsonData") String uploadData) {}

Here is the error I'm getting

Here is the stackTrace:

    SEVERE: Servlet.service() for servlet [ServletAdaptor] in context with path    [/TestProject] threw exception [com.sun.jersey.api.container.ContainerException: Exception obtaining parameters] with root cause
java.lang.NullPointerException
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:43)
at com.sun.jersey.multipart.impl.FormDataMultiPartDispatchProvider$FormDataInjectableValuesProvider.getInjectableValues(FormDataMultiPartDispatchProvider.java:115)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:126)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:154)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:163)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:71)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:111)

标签: java rest jersey
1条回答
Animai°情兽
2楼-- · 2019-08-22 03:30

Try:

@FormDataParam("FileUpload") InputStream fileInputStream

Instead of:

@FormDataParam("FileUpload") byte[] inputByteArray

According to FormDataParam API, the following is supported:

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA_TYPE)
public String postForm(
        @DefaultValue("true") @FormDataParam("enabled") boolean enabled,
        @FormDataParam("data") FileData bean,
        @FormDataParam("file") InputStream file,
        @FormDataParam("file") FormDataContentDisposition fileDisposition) {
    ...
}

From the javadoc:

Where the server consumes a multipart/form-data request entity body that contains one optional named body part "enabled" and two required named body parts data and file.

The optional part enabled is processed as a boolean value, if the part is absent then the value will be true.

The part data is processed as a JAXB bean and contains some meta-data about the following part.

The part file is a file that is uploaded, this is processed as an InputStream. Additional information about the file from the Content-Disposition header can be accessed by the parameter fileDisposition.

查看更多
登录 后发表回答