简体   繁体   English

使用REST API下载XML文件

[英]Download an XML file using a REST api

I need to write a REST api which would send an XML file as response. 我需要编写一个REST api,它将发送XML文件作为响应。 I am confused with what MediaType should be defined as the response. 我对应将MediaType定义为响应感到困惑。

 @Produces(MediaType.APPLICATION_OCTET_STREAM)

or 要么

 @Produces(MediaType.APPLICATION_XML)

The API will be served from a GET request and I want the file to be downloaded in the client side. 该API将通过GET请求提供,我希望在客户端下载文件。

我将使用@Produces(MediaType.APPLICATION_XML) ,因为@Produces(MediaType.APPLICATION_OCTET_STREAM)application/octet-stream的常量,通常用于二进制类型(例如视频,音乐和东西)

I suggest dont send file directly. 我建议不要直接发送文件。 read file at server end and send the file content as a response. 在服务器端读取文件并发送文件内容作为响应。

so it will look like 所以看起来像

@GET
@Produce("application/xml")
    public Response sendData(){
      //code to read file and store in a string object

     return Response.built.ok(string object);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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