Java Jersey 415(不支持的媒体类型)

时间:2015-02-21 09:37:05

标签: java maven jersey-2.0

我对目前正在构建的REST接口有疑问。 Iam通常是前端专家,由于没有可用的资源,现在处理后端的东西。请耐心等待我: - )。

这是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个成员,并且表单剪切了stringyfied字符串,这导致后端出现问题。我也试过使用excelbuilder.js,但也没用。生成excel文件后我发送的字节流没有从Java后端正确解释。现在我想保存excel文件,然后立即触发回调中的下载。

但我一直得到415不支持的媒体类型错误。我也遇到了CORS过滤器问题,似乎无法正常工作。

有人可以帮忙吗?

提前致谢,

塞巴斯蒂安

1 个答案:

答案 0 :(得分:2)

似乎主要问题是您没有配置MessageBodyReader来处理JSON。您在类路径上有jersey-media-json-jackson,但您仍需要注册提供程序。这就是不支持的媒体类型的原因。如果泽西岛找不到提供商来处理转换,那么这就是您将获得的状态。

如果您使用的是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的默认(无参数)构造函数。杰克逊需要这个创造POJO。


<强>更新

所以我猜@Produces不是问题。我能够让它工作,只需修复主要问题,第一颗子弹和最后一颗子弹。而且我也改变了

String startDateStr = new Date().toString();
// TO
long startDateStr = new Date().getTime();

我猜(至少对于Windows我来说)这是一个糟糕的文件名格式。