1

我正在尝试编写一个自定义ExceptionMapper并且我想返回text/plain,但是Response返回的或者链中的某些东西坚持用<pre></pre>标签包装我的内容。

这是我尝试过的,我尝试过没有设置MediaType

final Response.ResponseBuilder rb = Response.status(500);
rb.entity(e.getCause().getMessage());
return rb.build();

我也尝试过设置MediaType.TEXT_PLAIN_TYPE

final Response.ResponseBuilder rb = Response.status(500);
rb.type(MediaType.TEXT_PLAIN_TYPE);
rb.entity(e.getCause().getMessage());
return rb.build();

在这两种情况下,我都会收到用 HTML 包装的异常消息:

<html>
   <head></head>
   <body>
     <pre style="word-wrap: break-word; white-space: pre-wrap;">
          com.google.appengine.api.oauth.InvalidOAuthParametersException:
     </pre>
   </body>
   <style type="text/css"></style>  
</html>  

我认为这可能是包装内容的浏览器,我在 Chrome 和 Safari 中得到了完全围绕纯文本输出包装的相同 HTML 块,但是当我使用curl.

[jhr@Blackintosh] [~]
curl http://funny-pages.appspot.com/image/all
com.google.appengine.api.oauth.InvalidOAuthParametersException: 

我该怎么做才能让它在浏览器中显示未修饰的内容?

4

1 回答 1

0

只要curl给你一个纯文本,很明显 HTML 包装来自谷歌浏览器。可以(应该)在客户端配置此行为。如果您想确保您的客户始终获得纯文本文件 - 将其作为MIME 附件发送。在这种情况下,Chrome 会保存一个文件,用户必须使用一些纯文本查看器打开它。

于 2013-02-17T13:50:47.383 回答