diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBean.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBean.java index 2335d0b140..e681735651 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBean.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBean.java @@ -93,27 +93,22 @@ public class EndpointMBean implements DynamicMBean { return invoke(operation, params); } - private Object invoke(JmxOperation operation, Object[] params) throws MBeanException { + private Object invoke(JmxOperation operation, Object[] params) + throws MBeanException, ReflectionException { try { String[] parameterNames = operation.getParameters().stream() .map(JmxOperationParameter::getName).toArray(String[]::new); Map arguments = getArguments(parameterNames, params); - Object result = invokeOperation(operation, arguments); + Object result = operation + .invoke(new InvocationContext(SecurityContext.NONE, arguments)); if (REACTOR_PRESENT) { result = ReactiveHandler.handle(result); } return this.responseMapper.mapResponse(result); } catch (InvalidEndpointRequestException ex) { - throw new IllegalArgumentException(ex.getMessage(), ex); - } - } - - private Object invokeOperation(JmxOperation operation, - Map arguments) throws MBeanException { - try { - return operation.invoke(new InvocationContext(SecurityContext.NONE, - arguments)); + throw new ReflectionException(new IllegalArgumentException( + ex.getMessage()), ex.getMessage()); } catch (Exception ex) { throw new MBeanException(translateIfNecessary(ex), ex.getMessage()); diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanTests.java index 4c0cd5d32a..481f9b85a9 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanTests.java @@ -30,6 +30,8 @@ import org.junit.rules.ExpectedException; import reactor.core.publisher.Mono; import org.springframework.beans.FatalBeanException; +import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException; +import org.springframework.boot.actuate.endpoint.InvocationContext; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.instanceOf; @@ -124,6 +126,23 @@ public class EndpointMBeanTests { bean.invoke("missingOperation", NO_PARAMS, NO_SIGNATURE); } + @Test + public void invokeWhenOperationIsInvalidShouldThrowException() + throws MBeanException, ReflectionException { + TestJmxOperation operation = new TestJmxOperation() { + @Override + public Object invoke(InvocationContext context) { + throw new InvalidEndpointRequestException("test failure", "test"); + } + }; + TestExposableJmxEndpoint endpoint = new TestExposableJmxEndpoint(operation); + EndpointMBean bean = new EndpointMBean(this.responseMapper, endpoint); + this.thrown.expect(ReflectionException.class); + this.thrown.expectCause(instanceOf(IllegalArgumentException.class)); + this.thrown.expectMessage("test failure"); + bean.invoke("testOperation", NO_PARAMS, NO_SIGNATURE); + } + @Test public void invokeWhenMonoResultShouldBlockOnMono() throws MBeanException, ReflectionException {