Spring Boot:从bygreSQL数据库下载文件作为bytea并以json描述返回

时间:2019-02-21 13:52:39

标签: json rest spring-boot get attachment

我正在尝试使用Spring Boot控制器从PostgreSQL数据库下载一个gzip文件(存储为bytea类型),但我还需要返回一个json对象来描述状态,消息和有关请求的一些信息。客户

例如:

{    "success" : true,    "status" : "APPROVED"    "message" : "Your request <id> has been successfully approved" }

这是我的控制器:

@GetMapping("/enroll/{id}")
@Timed
public ResponseEntity<?> getEnrollById(@PathVariable String id) {
    if (condition1) {
        Request request = requestRepository.findByrequestId(id);
        ResponseDetails responseDetails = new ResponseDetails();
        if (condition2) {
            responseDetails.setSuccess(true);
            if (request.getStatus().equals("status1")) {
                responseDetails.setStatus("status1");
                responseDetails.setMessage(
                        "Please wait, your the request " + id + " needs to be approved by an administrator");
            } else if (request.getStatus().equals("status2")) {
                responseDetails.setStatus("status2");
                responseDetails.setMessage("Your request " + id + " has been status2");

            } else {
                responseDetails.setStatus(request.getStatus().toUpperCase());
                responseDetails.setMessage("Your request" + id + "has been successfully approved");
            }
            String contentType = null ; 
            if(contentType == null) {
                contentType = "application/octet-stream";
            }
              return ResponseEntity.ok()
                        .contentType(MediaType.parseMediaType(contentType))
                        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"enrollment_" + request.getRequestId() + "\".gz")
                        .body(request.getMsp());  // ==> msp type : byte[]
                       **//i want to return my json object + gz file here** 
        } else {
            responseDetails.setSuccess(false);
            responseDetails.setCode("ENROLLMENT_REQUEST_NOT_FOUND");
            responseDetails.setMessage("The enrollment request " + id + " doesn't exist");
            return new ResponseEntity<ResponseDetails>(responseDetails, HttpStatus.NOT_FOUND);

        }

    } else {
        return new ResponseEntity<ResponseDetails>(new ResponseDetails(false, "USER_INVALID_PARAMS", null,
                "Invalid enrollment request params request_id", null, null, null), HttpStatus.BAD_REQUEST);
    }
}

谢谢您回答我:)

0 个答案:

没有答案