Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring ResponseEntity

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.

like image 762
Scott Avatar asked Jul 21 '11 19:07

Scott


2 Answers

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.

like image 143
Felby Avatar answered Sep 18 '22 14:09

Felby


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);
    }
like image 38
jared Avatar answered Sep 16 '22 14:09

jared