[英]download uploaded file using spring mvc abstractions (avoid using raw HttpServletResponse)
[英]Is there in spring mvc abstractions over HttpServletResponse?
當我使用spring mvc時,我不使用原始的HttpServletRequest
。 取而代之的是我使用Model
。
有類似的方法可以避免使用HttpServletResponse
嗎?
我不確定是否能收到您的問題。
Spring MVC從控制器方法返回的任何內容,異常處理程序(如果該方法引發了業務異常),攔截器,HttpMessageConverters等派生出HttpServletResponse。SpringMVC參考文檔的一半可能處理了該問題。
也許在Controllers中查看受支持的方法返回類型將回答您的問題?
同樣,您的Controller方法可以返回ResponseEntity<?>
類型,您可以像這樣使用:
@Controller
public class MyController {
@RequestMapping("/test")
public ResponseEntity<String> test() {
return ResponseEntity.status(HttpStatus.OK).header("X-Foo", "Bar").body("Hello World!");
}
@RequestMapping(value= "/other", produces = "application/json")
public ResponseEntity<User> other() {
User user = new User("firstName", "lastName");
// here, Spring MVC will use a configured HttpMessageConverter to convert your model
// instance to JSON, for example
return ResponseEntity.status(HttpStatus.OK).body(user);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.