how to throw a 403 error in Apache CXF - Java

不想你离开。 提交于 2019-12-21 20:46:44

问题


I am using the following code based on having an interceptor. When check returns true I want to throw a 403 error:

    @Override
    public void handleMessage(Message arg0) throws Fault {


        HttpServletRequest request = context.getHttpServletRequest();
        if(check(request)){
        // currently not working
                throw "Fault";
        }

I want to throw a 403 error. How do I go about doing that from this situation?

I am a little confused about how "throws fault" works.

Any help is appreciated


回答1:


CXF will default the status code to 500 for a Fault, but you can set it with Fault.setStatusCode. For example

Fault fault = new Fault(new Exception("Exception message"));
fault.setStatusCode(403);
throw fault;



回答2:


Have you considered throwing:

throw new javax.ws.rs.ForbiddenException();

It returns http 403.



来源:https://stackoverflow.com/questions/17354839/how-to-throw-a-403-error-in-apache-cxf-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!