multipart/mixed 未找到 MessageBodyWriter(泽西岛、Servicemix)

multipart/mixed MessageBodyWriter not found (Jersey, 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。

你能指出错误可能来自哪里吗,因为我已经阅读了所有文档和论坛,但仍然不知道该怎么做。

谢谢!

试试这个:在 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>