找不到multipart / mixed MessageBodyWriter(Jersey,Servicemix)

时间:2015-09-10 21:43:17

标签: java rest jersey multipart apache-servicemix

我在apache Servicemix上安装了Jersey RESTful服务。此服务的一种方法不起作用,错误日志如下所示:

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文件中包含所有必需的依赖项,我也已将jersey OSGI包部署到Servicemix。

请您指出错误可能来自何处,因为我已经阅读了所有文件和论坛,但仍然不知道该怎么做。

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:在init-param下为ServletContainer创建一个MultiPartFeature条目

<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>