简体   繁体   中英

HttpStatus with ResponseEntity and pdf

I have a spring mvc controller which produces an "application/pdf" to download a generated pdf. I want to return always the pdf and also return different HTTP codes depending on certain conditions.

I tried to return a ResponseEntity<> object but always response is 200 with the PDF (always the pdf is generated, but I need different status responses).

@RequestMapping(value = "/obtain/downloadPDF", method = RequestMethod.GET, produces = "application/pdf")
@ResponseBody
public ResponseEntity<?> getPdfFile(HttpServletResponse response,HttpServletRequest request) throws IOException{

    response.setHeader("Content-disposition", "inline; filename=\"" +"file.pdf" +"\"");
    String error = getPDF(response, request);
    logger.debug("PDF error : " + StringUtils.defaultIfEmpty(error,"null") );
    return new ResponseEntity<>("",HttpStatus.INTERNAL_SERVER_ERROR);
}

Note : Inside getPDF method, is just the code which generates the pdf and write it to the response.getOutputStream() .

The idea behing is that I want to return ALWAYS the pdf in the outputstream, but different result codes (200, 500, etc). Event when I return 500, I want to return a PDF. Is it possible?

Any help will be appreciated.

I am not 100% sure about this, but I think the method is returning whatever is in the HttpServletResponse , rather that the object you are returning.

I think you should experiment with HttpServletResponse.getWriter().write(responseEntity.getBody()) and HttpServletResponse.setHttpStatus(responseEntity.getHttpStatus()) , or just the equivalent hard-coded values...

Also, are you seeing the errors and the 200 status alongside each other? If so, try parameterizing the entity as a String - whatever Throwable you put in there should still work fine.

Please let me know if you try that and it doesn't work so I can edit or remove this answer - I haven't actually tried this yet, but interested to find out why that code is not working.

The ideal way is to set response status ( response.setStatus(statusCode) ) in getPDF() which return the String message which you can show to the client. Now to you can get the status using response.getStatus() and can give a response to the client. You told that you want to download PDF file no matter which status code is there, below code does the same. But in my opinion when there some conditions which are not satisfying then you should not return PDF file.

As I don't know for which condition you want Status 500, I have created one sample which randomly generates the number and according to the condition it sets the response code.

@RequestMapping(value = "/obtain/downloadPDF", method = RequestMethod.GET, produces = "application/pdf")
    public ResponseEntity<?> getPdfFile(HttpServletResponse response,HttpServletRequest request) throws IOException{

        response.setHeader("Content-disposition", "inline; filename=\"" +"file.pdf" +"\"");
        String message = getPDF(response, request);
        System.out.println("statusCodeString: "+message + "Status: "+response.getStatus());

        return new ResponseEntity<>(message,(response.getStatus() == 500 ? HttpStatus.INTERNAL_SERVER_ERROR : HttpStatus.OK));
    }

    private String getPDF(HttpServletResponse response, HttpServletRequest request) {
        int min = 0;
        int max = 10;
        int num = (int) min + (int)(Math.random() * ((max - min) + 1));
        System.out.println("Number: "+num);
        if(num<=5)
        {
            response.setStatus(200);
            return "200 <message for 200>";
        }
        else
        {
            response.setStatus(500);
            return "500 <message for 500>";
        }       
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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