Java 未找到多部分/混合MessageBodyWriter(Jersey,Servicemix)
我在ApacheServiceMix上安装了Jersey RESTful服务。此服务的一个方法不起作用,错误日志如下所示:Java 未找到多部分/混合MessageBodyWriter(Jersey,Servicemix),java,rest,jersey,multipart,apache-servicemix,Java,Rest,Jersey,Multipart,Apache Servicemix,我在ApacheServiceMix上安装了Jersey RESTful服务。此服务的一个方法不起作用,错误日志如下所示: @Authenticated @Path("/floor") @POST @Consumes("application/json") @Produces("multipart/mixed") public MultiPart getFloorHeatMap(@NotNull(message = "You must provide a floor.") @Valid Floo
@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);
}
}
2015-09-10 12:12:25374错误tp1599275925-203写入器接收器执行器250-org.glassfish.jersey.core.jersey-common-2.17.0 |未找到媒体类型=multipart/mixed、类型=class org.glassfish.jersey.media.multipart.FormDataMultiPart、genericType=class org.glassfish.jersey.media.multipart.multipart的MessageBodyWriter。
方法如下所示:
@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的init param下为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>
ServletContainer将ResourceConfig作为参数。您可以在ResourceConfig中进行类型安全的联合配置,而不依赖于字符串。这样,您实际上就知道您是否拥有所有必需的依赖项。现在我们不知道。Spring属性没有编译错误,并且您实际上没有向我们显示您的依赖项。我会仔细检查pom.xml中是否有org.glassfish.jersey.media:jersey media多部分依赖项。另外,我在扩展ResourceConfig的应用程序类中使用了registerMultiPartFeature.class调用,但我想注册MultipartFeature的方法也应该有效。