就像标题所说的那样. @WebService( targetNamespace = "http://com.lalaland.TestWs", portName = "TestWs", serviceName = "TestWs")public class TestWs implements TestWsInterface { @EJB(name="validator") private ValidatorLocal validator;
@WebService( targetNamespace = "http://com.lalaland.TestWs", portName = "TestWs", serviceName = "TestWs") public class TestWs implements TestWsInterface { @EJB(name="validator") private ValidatorLocal validator; @WebMethod(operationName = "getStuff") public List<StuffItem> getStuff(@WebParam(name = "aaa")String aaa, @WebParam(name = "bbb")int bbb ) { if ( ! validator.check1(...) ) return HTTP code 403 <------------ Here if ( ! validator.check2(...) ) return HTTP code 404 <------------ Here if ( ! validator.check3(...) ) return HTTP code 499 <------------ Here return good list of Stuff Items }
无论如何我可以让方法按需返回特定的HTTP代码吗?我知道一些东西,如身份验证,内部服务器错误等,使得WS方法返回500和auth错误,但我希望能够按照业务逻辑发送这些错误.
有人这样做过吗?使用jax-WS已经有一段时间了,这是我第一次有这种需求,尝试搜索它并且无法在任何地方找到答案.
谢谢
只获取javax.servlet.http.HttpServletResponse的当前实例并发送错误.@WebService public class Test { private static final Logger LOG = Logger.getLogger(Test.class.getName()); @Resource private WebServiceContext context; @WebMethod(operationName = "testCode") public String testCode(@WebParam(name = "code") int code) { if (code < 200 || code > 299) { try { MessageContext ctx = context.getMessageContext(); HttpServletResponse response = (HttpServletResponse) ctx.get(MessageContext.SERVLET_RESPONSE); response.sendError(code, code + " You want it!"); } catch (IOException e) { LOG.severe("Never happens, or yes?"); } } return code + " Everything is fine!"; } }
另见List of HTTP status codes – Wikipedia, the free encyclopedia