JAX RS Jersey Spring Boot REST 初始化失败 API

Initialization failure on JAXRS Jersey SpringBoot REST API

使用 multipart/form-data 的简单图像上传控制器无法初始化。

控制器代码

    @POST
    @Path("/image")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public Response uploadImage( @FormDataParam("image") InputStream fileInputStream )
            throws Exception{

        BufferedImage image = ImageIO.read(fileInputStream);
        System.out.println("uploaded image");

        return Response.ok("done").build();
    }

https://github.com/snambi/jersey-server/blob/master/src/main/java/org/github/api/test/jerseyserver/ImageController.java

当 Jersey 初始化控制器时,第一个请求发生错误。

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 javax.ws.rs.core.Response org.github.api.test.jerseyserver.ImageController.uploadImage(java.io.InputStream) throws java.lang.Exception 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 org.github.api.test.jerseyserver.ImageController, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@7515f97e]}, definitionMethod=public javax.ws.rs.core.Response org.github.api.test.jerseyserver.ImageController.uploadImage(java.io.InputStream) throws java.lang.Exception, parameters=[Parameter [type=class java.io.InputStream, source=image, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:375) ~[jersey-server-2.35.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler.lambda$initialize(ApplicationHandler.java:297) ~[jersey-server-2.35.jar:na]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292) ~[jersey-common-2.35.jar:na]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:274) ~[jersey-common-2.35.jar:na]
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:232) ~[jersey-common-2.35.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:296) ~[jersey-server-2.35.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:261) ~[jersey-server-2.35.jar:na]
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311) ~[jersey-container-servlet-core-2.35.jar:na]
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154) ~[jersey-container-servlet-core-2.35.jar:na]
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:347) ~[jersey-container-servlet-core-2.35.jar:na]
    at javax.servlet.GenericServlet.init(GenericServlet.java:158) ~[tomcat-embed-core-9.0.62.jar:4.0.FR]
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1164) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:804) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:128) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:890) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1743) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.62.jar:9.0.62]
    at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]

似乎是与配置或依赖项相关的问题。还有其他人遇到过这个问题并找到了解决方案吗?

源代码位于 https://github.com/snambi/jersey-server

实际的 jersey multipart 需要额外的依赖和配置:https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest/media.html#multipart

  1. jersey-media-multipart
  2. 的依赖关系
  3. 注册MultiPartFeature.class

有关详细信息,请参阅上述文档