简体   繁体   中英

multipart/mixed MessageBodyWriter not found (Jersey, Servicemix)

I have a Jersey RESTful service installed on apache Servicemix. One method of this service does not work, the error log looks like this:

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.

Method looks like this:

@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);
    }

}

I am starting my application this way:

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();

I have all necessarry dependencies in my .pom file, also I have deployed jersey OSGI bundles to the Servicemix.

Could you please point me where the error may come from, because i have already read all the documentations and forums and still have no idea what to do.

Thank you!

Try this out: Make an entry for MultiPartFeature under init-param for 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM