简体   繁体   English

我想从java中的REST API返回XML格式的响应

[英]I want to return response in XML form from REST API in java

I have REST API in java that take image file and save on the server i want to return the path of that uploaded image in XML form but don't know how to do it.Currently it returns the response as a string in the browser. 我在java中使用REST API来获取图像文件并保存在服务器上我希望以XML格式返回上传图像的路径,但不知道如何操作。目前它在浏览器中将响应作为字符串返回。

Here is my code. 这是我的代码。

package com.javacodegeeks.enterprise.rest.jersey;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

@Path("/files")
public class JerseyFileUpload {

    private static final String SERVER_UPLOAD_LOCATION_FOLDER = "/home/hassan/Downloads/";

    /**
     * Upload a File
     */

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
            @FormDataParam("file") InputStream fileInputStream,
            @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {

        String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();

        // save the file to the server
        saveFile(fileInputStream, filePath);

        String output = "File saved to server location : " + filePath;

        return Response.status(200).entity(output).build();

    }

    // save uploaded file to a defined location on the server
    private void saveFile(InputStream uploadedInputStream,
            String serverLocation) {

        try {
            OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
            int read = 0;
            byte[] bytes = new byte[1024];

            outpuStream = new FileOutputStream(new File(serverLocation));
            while ((read = uploadedInputStream.read(bytes)) != -1) {
                outpuStream.write(bytes, 0, read);
            }
            outpuStream.flush();
            outpuStream.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

}

If you want to return xml from Rest, try to create Object with some fields. 如果要从Rest返回xml,请尝试使用某些字段创建Object。 and Object and field will have @XmlRootElement @XmlElement and put @Produces("application/xml") on top of method signature. 对象和字段将具有@XmlRootElement @XmlElement并将@Produces(“application / xml”)放在方法签名之上。

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces("application/xml")
    public Response uploadFile(...){
         //body
   }

also you can use @produces(MediaType.APPLICATION_XML) instead of @Produces("application/xml") . 您也可以使用@produces(MediaType.APPLICATION_XML)代替@Produces("application/xml") both are same. 两者都是一样的。

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

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