如何正确地将HTTP消息发送到客户端 [英] How to properly send an HTTP message to the client

查看:268
本文介绍了如何正确地将HTTP消息发送到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java的RESTful Web服务。如果出现问题,我需要一种向客户端发送错误消息的好方法。

I'm working on a RESTful web service in Java. I need a good way to send error messages to the client if something's wrong.

根据 Javadoc HttpServletResponse.setStatus (int status,String message)由于消息参数的含义不明确而被弃用

According to the Javadoc, HttpServletResponse.setStatus(int status, String message) is deprecated "due to ambiguous meaning of the message parameter."

是有一种设置状态消息或原因短语的首选方式响应? sendError(int,String)方法不会这样做。

Is there a preferred way to set the status message or "reason phrase" of the response? The sendError(int, String) method doesn't do it.

编辑:
澄清,我想修改HTTP状态行,即HTTP / 1.1 404 Not Found,而不是正文内容。具体来说,我想发送回复,如HTTP / 1.1 400 Missing customerNumber参数

To clarify, I want to modify the HTTP status line, i.e. "HTTP/1.1 404 Not Found", not the body content. Specifically, I'd like to send responses like "HTTP/1.1 400 Missing customerNumber parameter".

推荐答案

我认为任何RESTful客户端都不会期望查看原因,以找出问题所在;我见过/使用过的大多数RESTful服务都会在响应正文中发送标准状态信息和扩展消息。 sendError(int,String)非常适合这种情况。

I don't think any RESTful client would expect to look at the reason phrase to figure out what went wrong; most RESTful services I've seen/used will send the standard status info and an expanded message in the body of the response. sendError(int, String) is ideal for that situation.

这篇关于如何正确地将HTTP消息发送到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆