REST- Jersey - 异常获取参数
REST- Jersey - Exception obtaining parameters
这是我的 ajax 电话:
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 方法签名:
@Path("/Uploader")
@POST
@Consumes('multipart/form-data')
public String validateAndUpload(@FormDataParam("FileUpload") byte[] inputByteArray,
@Context HttpServletRequest request,
@FormParam("jsonData") String uploadData) {}
这是我遇到的错误
这里是堆栈跟踪:
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)
尝试:
@FormDataParam("FileUpload") InputStream fileInputStream
而不是:
@FormDataParam("FileUpload") byte[] inputByteArray
根据FormDataParam
API,支持以下内容:
@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) {
...
}
来自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
.
这是我的 ajax 电话:
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 方法签名:
@Path("/Uploader")
@POST
@Consumes('multipart/form-data')
public String validateAndUpload(@FormDataParam("FileUpload") byte[] inputByteArray,
@Context HttpServletRequest request,
@FormParam("jsonData") String uploadData) {}
这是我遇到的错误
这里是堆栈跟踪:
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)
尝试:
@FormDataParam("FileUpload") InputStream fileInputStream
而不是:
@FormDataParam("FileUpload") byte[] inputByteArray
根据FormDataParam
API,支持以下内容:
@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) {
...
}
来自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 partsdata
andfile
.The optional part
enabled
is processed as aboolean
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 anInputStream
. Additional information about the file from theContent-Disposition
header can be accessed by the parameterfileDisposition
.