如何在Mule中获取REST服务的HTTP状态码

如何在Mule中获取REST服务的HTTP状态码,mule,mule-studio,Mule,Mule Studio,我有以下骡子流程:- <jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/> <mule-ss:security-manager> <mule-ss:delegate-security-provider name="memory-provider"

我有以下骡子流程:-

<jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>

    <mule-ss:security-manager>
        <mule-ss:delegate-security-provider name="memory-provider" delegate-ref="authenticationManager" />
    </mule-ss:security-manager>
    <spring:beans>
        <ss:authentication-manager alias="authenticationManager">
            <ss:authentication-provider>
                <ss:user-service id="userService">
                    <ss:user name="username" password="password" authorities="ROLE_ADMIN" />
                </ss:user-service>
            </ss:authentication-provider>
        </ss:authentication-manager>
    </spring:beans>

<flow name="testFlow1" doc:name="testFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8083" doc:name="HTTP">
  <mule-ss:http-security-filter realm="realm" />
</http:inbound-endpoint>

 <jersey:resources doc:name="REST">
    <component class="MyRest" />
  </jersey:resources>

 <logger message="Done !!!!!" level="INFO" doc:name="Logger"/>

     <!-- http://username:password@localhost:8083/myrest   or   http://localhost:8083/myrest and in this case we need to provide username and password from browser-->

 <catch-exception-strategy>
    <choice >
     <when expression="#[exception.getCauseException().getClass().getName()=='org.springframework.security.authentication.BadCredentialsException']">
     <logger level="INFO" message="Authentication failed exception: #[exception.getCauseException().getClass().getName()]"/>
     <set-property propertyName="http.status" value="401"/>
     <http:response-builder status="401" doc:name="Status code = 401" doc:description="Authentication failed"/>
      </when>
        <when expression="#[exception.getCauseException().getClass().getName()=='org.springframework.security.access.AccessDeniedException']">
      <logger level="INFO" message="Access denied exception: #[exception.getCauseException().getClass().getName()]"/>
      <set-property propertyName="http.status" value="405"/>
     <http:response-builder status="405" doc:name="Status code = 405" doc:description="Authorization exception - Access denied"/>
     </when>
      <otherwise>
      <logger message="An exception has been caught: #[exception.getCauseException().getClass().getName()]" level="ERROR" doc:name="Exception Thrown"/>
     <set-payload value="Error detected while processing" doc:name="Prepare response for client"/>
     <set-property propertyName="http.status" value="500"/>
     <http:response-builder status="500" doc:name="Status code = 500" doc:description="Exception - Sending a 500 Http Status code as Response"/>
     </otherwise>
      </choice>
  </catch-exception-strategy>

    </flow>
我想知道为什么它会产生这样的异常

但是如果我从代码中删除异常块,并点击
http://localhost:8083/myrest
它工作正常,按预期询问用户名和密码,并生成预期结果

但我想知道,当我在代码中使用异常块时,它为什么会生成异常。。
实际上,我使用异常块的目的是在不同的情况下获取和控制不同的http状态

要检索http代码,可以使用过滤器并在流中抛出异常。这里有一个例子:

<message-filter throwOnUnaccepted="true">
   <message-property-filter pattern="message.inboundProperties['http.status'] >= 400" />
</message-filter>

要检索HTTP代码,可以使用筛选器并在流中引发异常。这里有一个例子:

<message-filter throwOnUnaccepted="true">
   <message-property-filter pattern="message.inboundProperties['http.status'] >= 400" />
</message-filter>

这里有两件事在起作用:

  • Jersey资源引发的异常

    此类异常必须由一个或多个自定义JAX-RS异常处理程序处理,这些异常处理程序通过一个或多个
    jersey:exception映射器
    元素注册到
    jersey:resources

  • mule ss:http安全筛选器引发的异常

    这些异常是由Mule在JAX-RS范围之外抛出的,因此它们必须由
    catch exception strategy
    元素处理,正如您在问题中所做的那样


  • 拥有这种错误处理组合应该可以满足您的需要。

    这里有两件事在起作用:

  • Jersey资源引发的异常

    此类异常必须由一个或多个自定义JAX-RS异常处理程序处理,这些异常处理程序通过一个或多个
    jersey:exception映射器
    元素注册到
    jersey:resources

  • mule ss:http安全筛选器引发的异常

    这些异常是由Mule在JAX-RS范围之外抛出的,因此它们必须由
    catch exception strategy
    元素处理,正如您在问题中所做的那样


  • 拥有这种错误处理组合应该可以满足您的需要。

    在获得相同的异常之前,仍然会获得相同的异常感谢David为您的响应感谢David为您的响应