我正在尝试使用java rest服务上传文件,我认为我的服务方法有参数问题。
我有这个堆栈跟踪:
12:56:24,030 SEVERE [org.glassfish.jersey.internal.Errors] (MSC service thread 1-6) Following issues have been detected:
WARNING: No injection source found for a parameter of type public java.lang.String org.vozCiudadana.restService.RestUploadService.uploadFile(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) throws java.text.ParseException at index 0.
12:56:24,031 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./vozCiudadanaWAR: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./vozCiudadanaWAR: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.8.0_65]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.8.0_65]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.8.0_65]
Caused by: 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 java.lang.String org.vozCiudadana.restService.RestUploadService.uploadFile(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) throws java.text.ParseException at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class org.vozCiudadana.restService.RestUploadService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@39b949c8]}, definitionMethod=public java.lang.String org.vozCiudadana.restService.RestUploadService.uploadFile(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) throws java.text.ParseException, parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:502)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:166)
at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:327)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:324)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:336)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:117)
at org.wildfly.extension.undertow.security.RunAsLifecycleInterceptor.init(RunAsLifecycleInterceptor.java:79)
at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:103)
at io.undertow.servlet.core.ManagedServlet$DefaultInstanceStrategy.start(ManagedServlet.java:220)
at io.undertow.servlet.core.ManagedServlet.createServlet(ManagedServlet.java:125)
at io.undertow.servlet.core.DeploymentManagerImpl.start(DeploymentManagerImpl.java:509)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:88)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
... 3 more
12:56:24,044 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "vozCiudadanaWAR.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.undertow.deployment.default-server.default-host./vozCiudadanaWAR" => "org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./vozCiudadanaWAR: Failed to start service
Caused by: 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 java.lang.String org.vozCiudadana.restService.RestUploadService.uploadFile(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) throws java.text.ParseException at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class org.vozCiudadana.restService.RestUploadService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@39b949c8]}, definitionMethod=public java.lang.String org.vozCiudadana.restService.RestUploadService.uploadFile(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) throws java.text.ParseException, parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}']"}}
12:56:24,087 INFO [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "vozCiudadanaWAR.war" (runtime-name : "vozCiudadanaWAR.war")
12:56:24,089 INFO [org.jboss.as.controller] (Controller Boot Thread) JBAS014774: Service status report
JBAS014777: Services which failed to start: service jboss.undertow.deployment.default-server.default-host./vozCiudadanaWAR: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./vozCiudadanaWAR: Failed to start service
12:56:24,165 INFO [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
12:56:24,166 INFO [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
12:56:24,166 ERROR [org.jboss.as] (Controller Boot Thread) JBAS015875: WildFly 8.2.1.Final "Tweek" started (with errors) in 14871ms - Started 271 of 330 services (2 services failed or missing dependencies, 92 services are lazy, passive or on-demand)
12:56:24,331 INFO [org.jboss.weld.deployer] (MSC service thread 1-7) JBAS016009: Stopping weld service for deployment vozCiudadanaWAR.war
12:56:24,357 INFO [org.jboss.as.jpa] (ServerService Thread Pool -- 14) JBAS011403: Stopping Persistence Unit Service 'vozCiudadanaWAR.war#OGMvozCiudadana'
12:56:24,359 INFO [org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider] (ServerService Thread Pool -- 14) OGM001202: Closing connection to MongoDB
12:56:24,445 INFO [org.jboss.as.server.deployment] (MSC service thread 1-6) JBAS015877: Stopped deployment vozCiudadanaWAR.war (runtime-name: vozCiudadanaWAR.war) in 132ms
12:56:24,723 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS018558: Undeployed "vozCiudadanaWAR.war" (runtime-name: "vozCiudadanaWAR.war")
12:56:24,725 INFO [org.jboss.as.controller] (DeploymentScanner-threads - 2) JBAS014774: Service status report
JBAS014775: New missing/unsatisfied dependencies:
service jboss.deployment.unit."vozCiudadanaWAR.war".component."com.sun.faces.config.ConfigureListener".START (missing) dependents: [service jboss.deployment.unit."vozCiudadanaWAR.war".deploymentCompleteService]
service jboss.deployment.unit."vozCiudadanaWAR.war".component."javax.faces.webapp.FacetTag".START (missing) dependents: [service jboss.deployment.unit."vozCiudadanaWAR.war".deploymentCompleteService]
service jboss.deployment.unit."vozCiudadanaWAR.war".component."javax.servlet.jsp.jstl.tlv.PermittedTaglibsTLV".START (missing) dependents: [service jboss.deployment.unit."vozCiudadanaWAR.war".deploymentCompleteService]
service jboss.deployment.unit."vozCiudadanaWAR.war".component."javax.servlet.jsp.jstl.tlv.ScriptFreeTLV".START (missing) dependents: [service jboss.deployment.unit."vozCiudadanaWAR.war".deploymentCompleteService]
service jboss.deployment.unit."vozCiudadanaWAR.war".component."org.glassfish.jersey.servlet.ServletContainer".START (missing) dependents: [service jboss.deployment.unit."vozCiudadanaWAR.war".deploymentCompleteService]
service jboss.deployment.unit."vozCiudadanaWAR.war".component."org.jboss.weld.servlet.WeldInitialListener".START (missing) dependents: [service jboss.deployment.unit."vozCiudadanaWAR.war".deploymentCompleteService]
service jboss.deployment.unit."vozCiudadanaWAR.war".component."org.jboss.weld.servlet.WeldTerminalListener".START (missing) dependents: [service jboss.deployment.unit."vozCiudadanaWAR.war".deploymentCompleteService]
service jboss.undertow.deployment.default-server.default-host./vozCiudadanaWAR (missing) dependents: [service jboss.deployment.unit."vozCiudadanaWAR.war".deploymentCompleteService]
JBAS014777: Services which failed to start: service jboss.undertow.deployment.default-server.default-host./vozCiudadanaWAR
我也有这个pom依赖项:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.9</version>
</dependency>
</dependencies>
,这在web.xml中:
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.filter.LoggingFilter;
org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
和服务类:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
@Path("files")
public class RestUploadService {
private static final String FOLDER_PATH = "C:\\my_files\\";
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadFile(@FormDataParam("file") InputStream fis,
@FormDataParam("file") FormDataContentDisposition f
) throws ParseException {
OutputStream outpuStream = null;
String fileName = f.getFileName();
System.out.println("File Name: " + f.getFileName());
String filePath = FOLDER_PATH + fileName;
try {
int read = 0;
byte[] bytes = new byte[1024];
outpuStream = new FileOutputStream(new File(filePath));
while ((read = fis.read(bytes)) != -1) {
outpuStream.write(bytes, 0, read);
}
outpuStream.flush();
outpuStream.close();
} catch(IOException iox){
iox.printStackTrace();
} finally {
if(outpuStream != null){
try{outpuStream.close();} catch(Exception ex){}
}
}
return "File Upload Successfully !!";
}
}
有什么帮助吗?
编辑:
在那个版本之前,我总是这样称呼我的服务:
样本:http://192.168.1.250:8080/vozCiudadanaWAR/rest/vozCiudadana/hola_mundo
现在,我该如何调用我的服务和方法?