dtx*_*txd 1 java rest multipartform-data glassfish jersey-2.0
http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/ 我正在按照本指南操作并遇到问题.我有一些问题.
所有的依赖都必须对应吗?我的项目有一些org.glassfish.jersey依赖项,本指南建议使用org.sun.jersey.我是否必须使用与此相同的版本进行更改?
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.16</version>
Run Code Online (Sandbox Code Playgroud)
我有这个错误
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization. [[FATAL] No injection source found for a parameter of type public ***.***.****.common.dto.response.AbstractResponse ***.***.****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService.upload(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class ***.***.****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@90516e]}, definitionMethod=public ***.***.***.common.dto.response.AbstractResponse ***.***.*****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService.upload(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file1, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file1, defaultValue=null]], responseType=class ***.***.***.common.dto.response.AbstractResponse}, nameBindings=[]}']
Run Code Online (Sandbox Code Playgroud)
这是我的网络服务
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public AbstractResponse upload(@FormDataParam("file1") InputStream file1,
@FormDataParam("file1") FormDataContentDisposition filename1
) {
Run Code Online (Sandbox Code Playgroud)
这是我的电话:
$.ajax({
url: 'http://localhost:8080/******/webapi/m2m/upload',
data: fd,
processData: false,
contentType: 'multipart/form-data',
type: 'POST',
success: function(data){
alert(JSON.stringify(data));
return;
}
});
Run Code Online (Sandbox Code Playgroud)如果Web服务只有1个参数(FormData InputStream),则可以访问该服务.怎么解决?
谢谢你peeskillet的答案.多一点.
SEVERE: The web application [/linterm2m] created a ThreadLocal with key of type [org.jvnet.hk2.internal.PerLocatorUtilities$1] (value [org.jvnet.hk2.internal.PerLocatorUtilities$1@df94b1]) and a value of type [java.util.WeakHashMap] (value [{}]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak.
Run Code Online (Sandbox Code Playgroud)
如果您正在使用项目org.glassfish,那么您使用的com.sun是泽西岛2,而且您应该永远不要将两者混合使用.话虽这么说,你所面临的错误很可能是因为你没有注册MultipartFeature.当资源模型(资源方法)在启动时被验证为"正确性"时,如果未注册该特征,则该特征的特定注释是未知的,并且它就像没有注释一样.并且你不能有多个没有注释的方法参数.
如果您使用的是ResourceConfig,您可以简单地使用
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(MultiPartFeature.class);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是web.xml,则可<init-param>以为您注册的Jersey servlet 设置一个
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
Run Code Online (Sandbox Code Playgroud)
"我还想为Web服务添加另一个String参数.我该怎么办?"
您需要将其作为multipart请求的一部分,并且客户端需要确保将其作为multipart的一部分发送.在服务器端,只需添加另一个@FormDataParam("anotherString") String anotherString作为方法参数.至于客户端,我不知道jQuery会帮助解决这个问题.尚未测试,但您可以尝试此操作,显示附加的数据FormParam.这是Angular的一些东西,我自己构建了请求体.可能有点多,因为您可能不需要显式设置内容类型.
| 归档时间: |
|
| 查看次数: |
3939 次 |
| 最近记录: |