java 有没有办法模拟 HTTP 错误响应 5xx 和 4xx
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9862379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any way to simulate HTTP Error responses 5xx and 4xx
提问by Harshdeep
I am developing a RESTful service which has two severs talking to each other, which may be named as external and internal. Internal registers requests to external for further processing. Only external is accessible to users.
我正在开发一个 RESTful 服务,它有两个相互通信的服务器,可以命名为外部和内部。内部寄存器请求外部进一步处理。用户只能访问外部。
For testing reliability my link between internal and external server, I want to simulate HTTP error as returned by external to internal. Is there any easy way to do so or I'll have to hardcode the response being sent by external to internal for testing mainly 5xx and 4xx errors.
为了测试内部和外部服务器之间的链接的可靠性,我想模拟外部到内部返回的 HTTP 错误。有什么简单的方法可以这样做,或者我必须对由外部发送到内部的响应进行硬编码,以主要测试 5xx 和 4xx 错误。
Server I am using is JBoss if at all this info is needed.
如果需要这些信息,我正在使用的服务器是 JBoss。
On searching Google, I found this data for iPlanet
在搜索谷歌时,我找到了 iPlanet 的数据
1. edit the obj.conf or your virtual server specific obj.conf
1. 编辑 obj.conf 或您的虚拟服务器特定的 obj.conf
2. Add the following line under
2. 在下面添加以下行
Error code="200" fn="set-variable" error="503"
错误代码="200" fn="set-variable" error="503"
this would return 503 even for successful serving (which would cause 200 to be returned by the default).
即使服务成功,这也会返回 503(这将导致默认返回 200)。
I am looking for something similar for JBoss
我正在为JBoss寻找类似的东西
采纳答案by Perception
I am not aware of a JBoss configuration that will allow you to do this outside of the application. But its easy enough to setup a Resource within your application that would simulate this behavior and remove dependency on vendor specific application server:
我不知道允许您在应用程序之外执行此操作的 JBoss 配置。但是它很容易在你的应用程序中设置一个资源来模拟这种行为并消除对供应商特定应用程序服务器的依赖:
@GET @POST @PUT @DELETE
@Path("/echostatus/{statusCode}")
@Produces(MediaType.TEXT_PLAIN, MediaType.TEXT_HTML)
public Response echoStatus(@PathParam("statusCode") final Integer statusCode) {
return Response.status(statusCode.intValue()).build();
}
回答by Martin
In my opinion, to test this integration, it would be much easier to develop simple stub web app that will return 5xx
code for any URI. To make it more flexible, you can add some handles to be able to tweak behavior of this test app in run time (e.g. based on URI, on various parameters of the request, ...).
在我看来,要测试这种集成,开发简单的存根 Web 应用程序会更容易,该应用程序将返回5xx
任何 URI 的代码。为了使它更灵活,您可以添加一些句柄,以便能够在运行时调整此测试应用程序的行为(例如,基于 URI、请求的各种参数,...)。
I am not aware of any component in JBoss that will do the thing with rewriting status code, but it is simple to do it on your own. Just write your own Tomcat Valve and put it in server.xml
我不知道 JBoss 中的任何组件可以通过重写状态代码来完成这项工作,但是您自己很容易做到这一点。自己写个Tomcat Valve然后放进去就行了server.xml
import java.io.IOException;
import javax.servlet.ServletException;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;
public class RequestThroughputLimitValve extends ValveBase implements Lifecycle {
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// NOTE: THIS IS NOT COMPLETE IMPLEMENTATION
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
private int statusCode;
@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
// Pass the request further, if necessary
getNext().invoke(request, response);
// Tweak the response
response.setContentType("text/plain");
response.sendError(this.statusCode, "Forced error.");
}
// This will make the status code configurable in valve
public void setStatusCode(int statusCode) {
this.statusCode = statusCode
}
public int getStatusCode() {
return this.statusCode;
}
}
回答by AlexR
It is very easy to produce error 500. Just throw exception into web service method. JBoss will generate response code 500.
很容易产生500错误,只要在web服务方法中抛出异常即可。JBoss 将生成响应代码 500。
Other way it to use HTTP Resptonse API. Just set status as you want. If you want you can write this code in HTTP filter that can be installed for testing purposes only. This way you can simulate any HTTP status (both 400 or 500 series)
使用 HTTP 响应 API 的其他方式。只需根据需要设置状态。如果您愿意,您可以在 HTTP 过滤器中编写此代码,该过滤器只能用于测试目的。这样你就可以模拟任何 HTTP 状态(400 或 500 系列)