[英]Java Jersey 415 (Unsupported Media Type)
我对目前正在构建的REST接口Iam有疑问。 由于没有可用的资源,Iam通常是前端专家,现在处理后端的东西。 因此,请耐心等待我:-)。
这是Class Iam所说的:
/**
*
* @author User
*/
@Path("/excel")
public class Excel {
private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
private static final String SERVER_UPLOAD_LOCATION_FOLDER = "C://tmp/";
@POST
@Path("/export")
//@Consumes(MediaType.APPLICATION_JSON)
@Consumes({"application/xml","application/json"})
public Response writeFile(FileContent fileContent){
//Check if anything is null, if so return a server error code 500
String startDateStr = new Date().toString();
try {
JsonParser parser = new JsonParser();
JsonObject element = (JsonObject) parser.parse(fileContent.getContent());
String fileName = startDateStr + Integer.toString(new Random().nextInt(100)) + ".xls";
FileInformation fi = new FileInformation(
fileName,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
);
File file = new File(fileName);
WritableWorkbook workbook = Workbook.createWorkbook(file);
WritableSheet sheet = workbook.createSheet(fileName, 0);
int i = 0;
while(element.get(Integer.toString(i)) != null && !element.get(Integer.toString(i)).getAsJsonArray().isJsonNull()) {
JsonArray line = element.get(Integer.toString(i)).getAsJsonArray();
System.out.println(i);
for(int n = 0; n < line.size(); n++)
{
System.out.println(line.get(n).toString());
Label label = new Label(n,i, line.get(n).toString());
sheet.addCell(label);
}
i++;
}
workbook.write();
workbook.close();
Runnable r = new DeleteThread(file);
new Thread(r).start();
return Response.status(201).entity(fi).header("Access-Control-Allow-Origin", "*").build();
} catch (NullPointerException | IOException | WriteException e ) {
System.out.println(e);
return Response.status(500).build();
}
}
@GET
@Path("/export")
@Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
public Response serveFile(@QueryParam("fileName") String fileName){
File file = new File(fileName);
Response.ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition",
"attachment; filename=" + fileName);
return response.build();
}
@PUT
@Path("/import")
@Produces
public Response putFile() {
/*@FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();
FileHelper.saveFile(fileInputStream, filePath);*/
return Response.status(200).entity("Hallo Friend").build();
}
}
这是我的POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.range_plan_viz.rest</groupId>
<artifactId>jerseyrangeplan</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>jerseyrangeplan Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>rangePlanVizRest</finalName>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<server>TomcatServer</server>
<path>/restRangePlan</path>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
</project>
这是请求有效负载的映射器类:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.range_plan_viz.wrapper;
/**
*
* @author User
*/
public class FileContent {
private String content;
public FileContent(String content){
setContent(content);
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
}
这是响应有效负载的映射器:
package org.range_plan_viz.wrapper;
import javax.xml.bind.annotation.XmlRootElement;
/**
* Created by User on 12.02.2015.
*/
@XmlRootElement
public final class FileInformation {
private String fileName;
private String contentType;
/**
* Default constructor
* @param content The content of the excel file published by the UI
* @param fileName The name of the file that should be generated published by the UI
* @param contentType The contenType of the file
*/
public FileInformation(
String fileName,
String contentType
){
setFileName(fileName);
setContentType(contentType);
}
/**
* Getter of the fileName
* @return {String} The fileName of the file that should be generated
*/
public String getFileName() {
return fileName;
}
/**
* The setter of the fileName
* @param {String} fileName The fileName of the file that should be
* generated
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* The getter of the content type
* @return {String} The contentType of the file that should be generated
*/
public String getContentType() {
return contentType;
}
/**
* The setter of the content type
* @param {String} contentType The contentType of the app that should be
* generated
*/
public void setContentType(String contentType) {
this.contentType = contentType;
}
@Override
public String toString() {
return "FileInformation [fileName=" + getFileName() + ", contenType=" + getContentType() + "]";
}
}
这是测试它的JavaScript:
var a =[BigDataMock],
url = "http://localhost:8084/jerseyrangeplan/rest/excel/export"
$.ajax({
url: url,
method: 'post',
crossDomain: true,
contentType: "application/json",
dataType: "json",
data:{
content: JSON.stringify(a)
},
success: function(result, status, xhr) {
window.location.assign(url + "?fileName=" + result.fileName);
},
error: function (responseData, textStatus, errorThrown) {
console.log('POST failed.');
}
});
我要做的基本上是提取Excel前端的大型数据集,然后下载文件。 我已经尝试过表单方法,但是它仅适用于小型数据集。 我们的对象中有10000个成员,并且该表单将字符串分割成字符串,导致后端出现问题。 我也尝试使用excelbuilder.js,但这也不起作用。 从Java后端无法正确解释生成excel文件后发送的字节流。 现在,我要保存Excel文件,然后立即在回调中触发下载。
但是我一直收到415不支持的媒体类型错误。 我也遇到了CORS过滤器的问题,它似乎无法正常工作。
有人可以帮忙吗?
提前致谢,
塞巴斯蒂安
似乎主要的问题是您没有配置用于处理JSON的MessageBodyReader
。 您在类路径上有jersey-media-json-jackson
,但仍然需要注册该提供程序。 这就是为什么不支持的媒体类型。 如果Jersey找不到提供者来处理转换,这就是您将获得的状态。
如果您使用的是web.xml,则可以使用
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
jersey.web.stackoverflow, <!-- your package -->
org.codehaus.jackson.jaxrs <!-- jackson package -->
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
如果您使用的是ResourceConfig
类,则只需执行
public class AppConfig extends ResourceConfig {
public AppConfig() {
register(JacksonFeature.class);
}
}
解决此问题后可以期待什么
不确定您如何发送数据,我不知道它是什么BigDataMock
,但是如果杰克逊无法读取数据,则可能会收到400错误的请求
注释中也提到了,您的GET重定向方法中的@Produces
很有可能会遇到另一个问题。 不支持该内容类型,您将需要为其编写自定义MessageBodyWriter
,或者改为简单地使用application/octect-stream
。
您还需要FileContent
的默认(无参数)构造FileContent
。 杰克逊需要这个来创建POJO。
UPDATE
所以我猜@Produces
不是问题。 我能够解决问题,只需解决主要问题,第一个项目符号和最后一个项目符号。 我也改变了
String startDateStr = new Date().toString();
// TO
long startDateStr = new Date().getTime();
我猜(至少对于Windows用户而言),这是一种错误的文件名格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.