ResponseEntity <t> </t>的用法

时间:2012-01-22 16:00:51

标签: spring spring-mvc

我有以下代码(它是控制器中的方法)下载附件/文档:

@RequestMapping(value="downloadattachment.htm",method=RequestMethod.GET)
public ResponseEntity<Blob> downloadAttachment(@RequestParam("attachmentid")
        int attachmentId){
    //Attachment is a POJO.
    Attachment attachment= commonDao.getAttachment(attachmentId);

    HttpHeaders responseHeaders = new HttpHeaders();

    responseHeaders.setContentType(new MediaType(attachment.getContentType()));
    responseHeaders.set("Content-Disposition",
            "attachment; filename=\"" + attachment.getFileName() +"\"");

    return new ResponseEntity<Blob>(attachment.getFileData(), responseHeaders, HttpStatus.CREATED);
}

当我运行它时会出现以下错误:

  

The request sent by the client was syntactically incorrect ().

我正在使用瓷砖,所以我的视图解析器是:

<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>

它是否以某种方式与切片相关,因为所有视图解析都是通过切片定义进行的?

但我认为(根据spring引用),当使用@ResponseBodyResponseEntity<T>时,处理程序方法的返回类型直接写入HTTP响应主体(而不是放在模型中,或解释为视图名称。)

1 个答案:

答案 0 :(得分:2)

问题在于请求而不是响应。

@RequestParam("attachmentid") int attachmentId

我认为您没有从客户端发送参数attachmentId,或者请求将转到其他控制器方法。