0

我正在编写一个自定义 Springboot Actuator Endpoint,我想返回一个 File。但是,我找不到返回 JSON 字符串以外的任何内容的方法。

我的端点是:

import java.io.File;
import java.net.URI;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.stereotype.Component;

@Component
public class MyEndPoint implements Endpoint<URI> {

   @Override
   public String getId() {
       return "custoendpoint";
   }

   @Override
   public boolean isEnabled() {
       return true;
   }

   @Override
   public boolean isSensitive() {
       return false;
   }

   @Override
   public URI invoke() {
       File f = new File("C:/temp/cars.csv");
       return f.toURI();    
   }
}

当我访问 localhost:8080/custoendpoint 时,我收到了我的文件的路径...

任何想法 ?

4

1 回答 1

0

而不是实施Endpoint你应该实施MvcEndpoint. 然后,您可以访问HttpServletResponse可以将文件内容复制到的位置。例如:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

class CustomEndpoint implements MvcEndpoint {

    @Override
    public String getPath() {
        return "/custom";
    }

    @Override
    public boolean isSensitive() {
        return false;
    }

    @Override
    public Class<? extends Endpoint<?>> getEndpointType() {
        return null;
    }

    @RequestMapping(method = RequestMethod.GET)
    public void invoke(HttpServletResponse response)
            throws ServletException, IOException {
        FileCopyUtils.copy(new FileInputStream(new File("c:/temp/cars.csv")),
                response.getOutputStream());
    }

}
于 2016-05-13T13:21:22.743 回答