如何保持ResponseBody 204无内容响应?

时间:2017-11-21 10:02:09

标签: java spring spring-mvc httpresponse

我想返回<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js" integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script> <script src="https://cdn.steemjs.com/lib/latest/steem.min.js"></script> </head> <body> <div class="container"> <div class="card-deck"> <div class="card mb-4"> <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap"> <div class="card-body"> <h4 class="card-title">1 Card title</h4> <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> <div class="card mb-4"> <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap"> <div class="card-body"> <h4 class="card-title">2 Card title</h4> <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> <div class="w-100 d-none d-sm-block d-md-none"><!-- wrap every 2 on sm--></div> <div class="card mb-4"> <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap"> <div class="card-body"> <h4 class="card-title">3 Card title</h4> <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> <div class="w-100 d-none d-md-block d-lg-none"><!-- wrap every 3 on md--></div> <div class="card mb-4"> <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap"> <div class="card-body"> <h4 class="card-title">4 Card title</h4> <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> <div class="w-100 d-none d-sm-block d-md-none"><!-- wrap every 2 on sm--></div> <div class="w-100 d-none d-lg-block d-xl-none"><!-- wrap every 4 on lg--></div> <div class="card mb-4"> <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap"> <div class="card-body"> <h4 class="card-title">5 Card title</h4> <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> <div class="w-100 d-none d-xl-block"><!-- wrap every 5 on xl--></div> <div class="card mb-4"> <img class="card-img-top img-fluid" src="//placehold.it/500x300" alt="Card image cap"> <div class="card-body"> <h4 class="card-title">6 Card title</h4> <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> <div class="w-100 d-none d-sm-block d-md-none"><!-- wrap every 2 on sm--></div> <div class="w-100 d-none d-md-block d-lg-none"><!-- wrap every 3 on md--></div> <div class="card mb-4"> <img class="card-img-top img-fluid" src="//placehold.it/500x270" alt="Card image cap"> <div class="card-body"> <h4 class="card-title">7 Card title</h4> <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> <div class="card mb-4"> <img class="card-img-top img-fluid" src="//placehold.it/500x300" alt="Card image cap"> <div class="card-body"> <h4 class="card-title">8 Card title</h4> <p class="card-text">This card has supporting text below as a natural lead-in to additional content.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> <div class="w-100 d-none d-sm-block d-md-none"><!-- wrap every 2 on sm--></div> <div class="w-100 d-none d-lg-block d-xl-none"><!-- wrap every 4 on lg--></div> <div class="card mb-4"> <img class="card-img-top img-fluid" src="//placehold.it/500x270" alt="Card image cap"> <div class="card-body"> <h4 class="card-title">Card title</h4> <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> <div class="w-100 d-none d-md-block d-lg-none"><!-- wrap every 3 on md--></div> <div class="card mb-4"> <img class="card-img-top img-fluid" src="//placehold.it/500x270" alt="Card image cap"> <div class="card-body"> <h4 class="card-title">10 Card title</h4> <p class="card-text">This is a wider card with supporting text below as a natural lead-in to additional content. This card has even longer content than the first to show that equal height action.</p> <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p> </div> </div> </div> </div> </body> http状态代码。虽然我想添加一个自定义错误消息,提供详细信息为什么哪里没有内容。

问题:我使用204 no content,并且在返回spring-mvc类型时,响应正文始终被移除并为客户端清空!

HttpStatus.NO_CONTENT

如果我将类型更改为@RestControllerAdvice public class ExeptionHandler { @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.NO_CONTENT) public Object handler(HttpServletRequest req, Exception e) { return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT); } } ,则错误消息将显示为响应正文。

如何用204实现相同的目标?

2 个答案:

答案 0 :(得分:4)

  

我想返回一个204无内容的HTTP状态代码。虽然我想添加一个自定义错误消息,但会详细说明为何没有内容。

你不能。

  

问题:我使用的是Spring MVC,当返回HttpStatus.NO_CONTENT类型时,响应主体总是被移除并为客户端清空!

这是预期的行为。

  

如果我将类型更改为HttpStatus.NOT_FOUND,则错误消息将显示为响应正文。我怎么能用204实现同样的目标呢?

您无法通过204实现目标。您误解(并且可能滥用)此类状态代码。 204的目的是表明服务器已成功完成请求,并且不会在响应有效内容中发送其他内容

了解如何在RFC 7231

中定义此状态代码
  

6.3.5. 204 No Content

     

204(无内容)状态代码表示服务器具有     成功完成了请求,并且没有其他要求     要在响应有效负载主体中发送的内容。 [...]

     

204响应由标头后的第一个空行终止     字段,因为它不能包含邮件正文。 [...]

根据具体情况,您最好在响应有效负载中使用空数组404200。这个answer可能会为您提供一些见解。

答案 1 :(得分:1)

204表示成功请求。它应该没有包含错误消息。

如果发生错误,则应使用其他HTTP代码。

请参阅https://httpstatuses.com/204

204响应由标题字段后面的第一个空行终止,因为它不能包含邮件正文。