English 中文(简体)
Spring ResponseEntity
原标题:Spring ResponseEntity

我有一个使用案例,即我需要把国防军归还给我们产生的用户。 看来,我需要做的是利用这一情况下的应急能力,但我有两点不很清楚。

  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?

页: 1 例:

@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);
    }

我真的希望不要在反应中 pull。 我的管制人员迄今没有这样做,我很仇恨,必须把它完全带进去。

最佳回答

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.

这可能赢得所有人的拖车,但以我们增加错误/状况信息的方式,我们无法使这些讯息在出现例外时得以坚持。

问题回答

请注意,这项工作在普春3.1中进行,但如原问题所要求,不能确定春季3.0.5左右。

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.

关于转口,你们都需要做的是改变返回类型,以反对:

@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);
    }

转头很容易——就你的手递法而言,只剩下<代码>的预支款:,如<代码>return “redirect: some where otherwise”。

不清楚为什么你对<代码>Response的反对。 是否有理由? 否则,如果你将PDF作为OutputStream。 HttpServletResponse Object, 然后,你实际上不需要从你的手法中归还PDF——你只是需要把PDF的溪流放在对策上,你可以在你的手法上添加这一内容。 见。 例如。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签