加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何使JAX-WS webservice响应特定的http代码

发布时间:2020-12-16 21:37:00 所属栏目:安全 来源:网络整理
导读:就像标题所说的那样. @WebService( targetNamespace = "http://com.lalaland.TestWs",portName = "TestWs",serviceName = "TestWs")public class TestWs implements TestWsInterface { @EJB(name="validator") private ValidatorLocal validator; @WebMethod
就像标题所说的那样.

@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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读