我在Spring MVC中有一个场景,我必须在handleRequest方法中显式抛出Exception。抛出异常作为来自Spring handleRequest方法的响应
例如,
public class AdminController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// for some request parameter this is run. For Eg. reqParam=xyz
undeployTables();
}
public void undeployTables() throws Exception {
throw new Exception("Undeploy Exception");
}
现在假设春天servlet和URL映射配置在web.xml中,当我试图通过URL http://localhost:8080/server/admin?reqParam=xyz
访问servlet中,我得到网页上的正确的错误。 java.lang.Exception: Undeploy failed....
但是,当我试着通过了HTTPClient访问码,即通过Java代码在客户端的响应是不是能赶上这个例外,只有我得到的是HTTP 401内部错误的响应object.But什么我想在客户端有一个Exception对象。
代码是:
HttpPost httppost = new HttpPost(url.toURI());
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
logger.info("executing request " + httppost.getURI()); //$NON-NLS-1$
// Create a response handler
HttpResponse response = httpclient.execute(httppost);// expect exception to be thrown here
statusCode = response.getStatusLine().getStatusCode();
能否请您让我知道的变化应该做到让客户端上的异常可能会被卡住。
我可以谷歌,但无法找到合适的答案。
什么如果servlet直接抛出异常? HTTPClient是否可以捕获异常作为响应?这不正确吗? – chaosguru 2012-07-20 08:18:40
不,它不是Java调用Java,它是Java调用Web服务 - Web服务恰好也是在Java中实现的......如果您想要返回错误,则需要使用HTTP来执行此操作。 – MattR 2012-07-20 10:30:22