开发者

Spring ResponseEntity

开发者 https://www.devze.com 2023-03-22 14:55 出处:网络
I have a use case where I need to return a PDF to a user which is generated for us.It seems that what I need to do is utilize the ResponseEntity in this case, but I have a couple of things which are n

I have a use case where I need to return a PDF to a user which is generated for us. It seems that what I need to do is utilize the ResponseEntity in this case, but I have a couple of things which are not very clear.

  1. How can I redirect the user -- let's pretend they don't have the permissions to access this page? How can I redirect them to a separate controller?
  2. Am I able to set the response encoding?
  3. Can I achieve either of these two without bringing in the HttpResponse as a parameter to my RequestMapping?

I'm using Spring 3.0.5. Example code below:

@Controller
@RequestMapping("/generate/data/pdf.xhtml")
public class PdfController {

    @RequestMapping
开发者_开发技巧    public ResponseEntity<byte []> generatePdf(@RequestAttribute("key") Key itemKey) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.valueOf("application/pdf"));

        if (itemKey == null || !allowedToViewPdf(itemKey)) {
            //How can I redirect here?
        }

        //How can I set the response content type to UTF_8 -- I need this
        //for a separate controller
        return new ResponseEntity<byte []>(PdfGenerator.generateFromKey(itemKey),
                                           responseHeaders,
                                           HttpStatus.CREATED);
    }

I'd really like to not pull in the Response... None of my controllers have done so thus far, and I'd hate to have to bring it in at all.


Note, this works in Spring 3.1, not sure about spring 3.0.5 as asked in the original question.

In your return ResponseEntity statement where you want to handle the redirect, just add in a "Location" header to the ResponseEntity, set the body to null and set the HttpStatus to FOUND (302).

HttpHeaders headers = new HttpHeaders();
headers.add("Location", "http://stackoverflow.com");

return new ResponseEntity<byte []>(null,headers,HttpStatus.FOUND);

This will keep you from having to change the return type of the controller method.


Regarding the redirect, all you need to do is change the return type to Object:

@Controller
@RequestMapping("/generate/data/pdf.xhtml")
public class PdfController {

    @RequestMapping
    public Object generatePdf(@RequestAttribute("key") Key itemKey) {
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.valueOf("application/pdf"));

        if (itemKey == null || !allowedToViewPdf(itemKey)) {
            return "redirect:/some/path/to/redirect"
        }

        //How can I set the response content type to UTF_8 -- I need this
        //for a separate controller
        return new ResponseEntity<byte []>(PdfGenerator.generateFromKey(itemKey),
                                           responseHeaders,
                                           HttpStatus.CREATED);
    }


Redirects are easy - for your handler method's return String, just prepend with redirect:, as in return "redirect:somewhere else".

Not sure why you're objecting to the Response object. Is there a reason? Otherwise, if you just stream the PDF as an OutputStream on the HttpServletResponse object, then you don't actually need to return the PDF from your handler method - you just need to set the PDF stream on the response, which you can add to your handler method's signature. See http://www.exampledepot.com/egs/javax.servlet/GetImage.html for an example.


Instead of dealing with redirecting (these are instances which we open in new windows / tabs) anyhow we decided to just display the error message they would have received.

This likely won't work for all, but with the way we add error / status messages we were unable to get those messages to persist on the view upon exception occurring.

0

精彩评论

暂无评论...
验证码 换一张
取 消