我正在使用JERSEY 1.8。当我的函数正常运行时它运行正常。但如果出现异常,则会抛出错误:
com.sun.jersey.api.MessageException: A message body writer for Java class java.util.HashMap, and Java type class java.util.HashMap, and MIME media type application/octet-stream was not found.
我尝试了所有解决方案,但它不会消失。
JAVA课程:
@GET
@Path("/{programId}/export")
public Response exportProgramData(@PathParam(PROGRAM_ID) String programId, @Context HttpServletRequest request) {
ResponseBuilder respBuilder = null;
Map<String, Object> response = new HashMap<String, Object>();
try {
String programResponse = programsFacade.getProgramReportAsCSV(programId, userId);
if (programResponse != null) {
String csvFileName = programsFacade.buildFileNameForReport(programId, userId) + CSV_EXTN;
respBuilder = Response.ok().entity(new ByteArrayInputStream(programResponse.getBytes()));
respBuilder.header(HTTP.CONTENT_TYPE, CONTENT_CSV_TYPE);
respBuilder.header(CONTENT_DISPOSITION, "attachment; filename=\"" + csvFileName + "\"");
NewCookie downloadCookie = new NewCookie("fileDownload", "true", "/", null, "no comment", 1073741823,
false);
respBuilder.cookie(downloadCookie);
logger.info(LOGGER_PREFIX + "Exported program response for program: " + programId);
} else {
String message = "There was an error while getting the program response.";
logger.error(LOGGER_PREFIX + message);
respBuilder = Response.ok().entity(message);
respBuilder.header(HTTP.CONTENT_TYPE, MediaType.TEXT_PLAIN);
NewCookie downloadCookie = new NewCookie("fileDownload", "false", "/", null, "no comment", 1073741823,
false);
respBuilder.cookie(downloadCookie);
}
} catch (WorkflowManagerException wme) {
response.put(SUCCESS, false);
response.put(HTTP_STATUS, WfManagerUtil.getResponseErrorCode(wme).getStatusCode());
response.put(MESSAGE, wme.getMessage());
throw new WebApplicationException(Response.status(WfManagerUtil.getResponseErrorCode(wme)).entity(response)
.build());
}catch (Exception ex) {
String message = "There was an error while getting the program response.";
logger.error(LOGGER_PREFIX + message, ex);
respBuilder = Response.ok().entity(message);
respBuilder.header(HTTP.CONTENT_TYPE, MediaType.TEXT_PLAIN);
NewCookie downloadCookie = new NewCookie("fileDownload", "false", "/", null, "no comment", 1073741823,
false);
respBuilder.cookie(downloadCookie);
}
return respBuilder.build();
}
POM:
<dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20080701</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.5.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<!-- REST Dependency -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
<!-- <scope>provided</scope> -->
</dependency>
n is provided by velocity dep but is not available as artifact -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
<!-- <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.8</version>
<!-- <scope>provided</scope> -->
</dependency>
如果出现任何WorkflowManagerException,它只会抛出此错误。
请帮忙
答案 0 :(得分:0)
尝试给予
respBuilder = Response.ok()。entity(new String(&#34;获取程序响应时出错。&#34;));
在异常捕获
中答案 1 :(得分:0)
假设您希望将HashMap
序列化为JSON ...
如果客户端未设置Accept: application/json
标头,则Jersey无法知道要将HashMap
序列化的媒体类型。因此,它默认尝试序列化为application/octet-stream
。并且 没有该媒体类型的作者HashMap
。您需要通过在响应中设置媒体类型来告诉Jersey
Response errorResponse = Response.status(..)
.entity(response)
.type("application/json")
.build();
throw new WebApplicationException(errorResponse);
还要确保在web.xml中配置jersey-json
以使用Jackson
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>