2015-09-10 71 views
0

我在Apache Servicemix上安装了Jersey RESTful服务。该服务无法正常工作的一种方法,错误日志是这样的:multipart/mixed MessageBodyWriter找不到(Jersey,Servicemix)

2015-09-10 12:12:25,374 | ERROR | tp1599275925-203 | WriterInterceptorExecutor | ? ? | 250 - org.glassfish.jersey.core.jersey-common - 2.17.0 | MessageBodyWriter not found for media type=multipart/mixed, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.MultiPart.

方法是这样的:

@Authenticated 
@Path("/floor") 
@POST 
@Consumes("application/json") 
@Produces("multipart/mixed") 
public MultiPart getFloorHeatMap(@NotNull(message = "You must provide a floor.") @Valid Floor floor) { 


    try { 
     Map<File, String> resp = null; 

     if (floor == null) { 
      throw new Exception("you must provide a floor"); 
     } 
     ValidatorFactory factory = Validation.byDefaultProvider() 
       .providerResolver(new OSGiServiceDiscoverer()) 
       .configure() 
       .buildValidatorFactory(); 
     Validator validator = factory.getValidator(); 
     Set<ConstraintViolation<Floor>> constraints = validator.validate(floor, Default.class); 

     for (ConstraintViolation<Floor> cv : constraints) { 
      throw new Exception(cv.getMessage()); 
     } 

     resp = template.requestBody(AppServerRouteBuilder.GET_HEATMAPS_ENDPOINT, floor, Map.class); 


     final MultiPart multipart = new FormDataMultiPart(); 

     if (resp != null) { 
      for (File file : resp.keySet()) { 
       multipart.bodyPart(new FileDataBodyPart(file.getName(), file, new MediaType("image", resp.get(file)))); 
      } 
      return multipart; 
      } else { 
      ServiceResponse response = new ServiceResponse(ResultCode.NOT_FOUND); 
      return new FormDataMultiPart().field("Response", "Failed to get HeatMaps", MediaType.TEXT_PLAIN_TYPE); 
     } 
    } catch (Exception e) { 
     LOG.error("EXCEPTION CAUGHT"); 
     LOG.error(e.getMessage(), e); 

     ServiceResponse response = new ServiceResponse(ResultCode.INTERNAL_ERROR, e.getMessage()); 
     return new FormDataMultiPart().field("Response", "Failed to get HeatMaps", MediaType.TEXT_PLAIN_TYPE); 
    } 

} 

我开始我的应用程序是这样的:

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 
     context.setContextPath("/"); 

     server = new Server(Integer.parseInt(getPort())); 
     server.setHandler(context); 

     ServletHolder sh = new ServletHolder(new ServletContainer()); 

     Map<String, String>map = new HashMap<>(); 
     map.put("jersey.config.server.provider.classnames", 
       "org.glassfish.jersey.jackson.JacksonFeature,org.glassfish.jersey.media.multipart.MultiPartFeature"); 
     map.put("jersey.config.server.provider.packages", "com.extremenetworks.rfplanner.wb.resources"); 
     map.put("jersey.config.server.jsonFeature", "JacksonFeature"); 
     map.put("org.glassfish.jersey.media.multipart.MultiPartFeature", "MultiPartFeature"); 
     map.put("jersey.config.beanValidation.enableOutputValidationErrorEntity.server", "true"); 
     sh.setInitParameters(map); 

     context.addServlet(sh, getPathSpec()); 
     server.start(); 

我在我的.pom文件中有所有必需的依赖关系,我也已经将Service Jersey的泽西OSGI包部署到了Servicemix。

请您指出错误可能来自哪里,因为我已阅读所有文档和论坛,但仍不知道该怎么办。

谢谢!

+1

'ServletContainer'需要'ResourceConfig'作为参数。您可以在'ResourceConfig'中进行类型安全的配置,而不是依赖于字符串。这样你就可以知道你是否拥有所有必需的依赖关系。现在我们不知道。 Spring属性没有编译错误,而且你实际上并没有向我们显示你的依赖关系。 –

+0

我会仔细检查一下,在pom.xml中有'org.glassfish.jersey.media:jersey-media-multipart'依赖项。另外,我在扩展'ResourceConfig'的Application类中使用'register(MultiPartFeature.class)'调用,但我猜你注册MultipartFeature的方法也应该起作用。 – jFrenetic

回答

0

尝试了这一点:请在初始化-参数有关MultiPartFeature的条目ServletContainer

<servlet> 
<servlet-name>jersey-servlet</servlet-name> 
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
<!-- Register resources and providers --> 
<init-param> 
<param-name>jersey.config.server.provider.packages</param-name> 
<param-value>com.mycompany.mypackage</param-value> 
</init-param> 
<init-param> 
<param-name>jersey.config.server.provider.classnames</param-name> 
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value> 
</init-param> 
<load-on-startup>1</load-on-startup> 
</servlet> 
相关问题