Support @PathVariable in annotated message handling methods
Prior to this commit, @SubscribeEvent @UnsubscribeEvent and @MessageMapping annotated message handling methods could only match a strict message destination. This commit adds a @PathVariable annotation and updates the message matching/handling process, since message handling methods can now match PathMatcher-like destinations and get path variables injected in parameters. Issue: SPR-10949
This commit is contained in:
committed by
Rossen Stoyanchev
parent
efa86e80d8
commit
fb586da673
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.messaging.handler.annotation.support;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.DefaultParameterNameDiscoverer;
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.handler.annotation.PathVariable;
|
||||
import org.springframework.messaging.simp.handler.AnnotationMethodMessageHandler;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link PathVariableMethodArgumentResolver} tests.
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class PathVariableMethodArgumentResolverTests {
|
||||
|
||||
private PathVariableMethodArgumentResolver resolver;
|
||||
|
||||
private MethodParameter paramAnnotated;
|
||||
private MethodParameter paramAnnotatedValue;
|
||||
private MethodParameter paramNotAnnotated;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
GenericApplicationContext cxt = new GenericApplicationContext();
|
||||
cxt.refresh();
|
||||
this.resolver = new PathVariableMethodArgumentResolver(new DefaultConversionService(), cxt.getBeanFactory());
|
||||
|
||||
Method method = getClass().getDeclaredMethod("handleMessage",
|
||||
String.class, String.class, String.class);
|
||||
this.paramAnnotated = new MethodParameter(method, 0);
|
||||
this.paramAnnotatedValue = new MethodParameter(method, 1);
|
||||
this.paramNotAnnotated = new MethodParameter(method, 2);
|
||||
|
||||
this.paramAnnotated.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
|
||||
GenericTypeResolver.resolveParameterType(this.paramAnnotated, PathVariableMethodArgumentResolver.class);
|
||||
this.paramAnnotatedValue.initParameterNameDiscovery(new DefaultParameterNameDiscoverer());
|
||||
GenericTypeResolver.resolveParameterType(this.paramAnnotatedValue, PathVariableMethodArgumentResolver.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void supportsParameter() {
|
||||
assertTrue(resolver.supportsParameter(paramAnnotated));
|
||||
assertTrue(resolver.supportsParameter(paramAnnotatedValue));
|
||||
assertFalse(resolver.supportsParameter(paramNotAnnotated));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolveArgument() throws Exception {
|
||||
Map<String,Object> pathParams = new HashMap<String,Object>();
|
||||
pathParams.put("foo","bar");
|
||||
pathParams.put("name","value");
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0])
|
||||
.setHeader(AnnotationMethodMessageHandler.PATH_TEMPLATE_VARIABLES_HEADER, pathParams).build();
|
||||
Object result = this.resolver.resolveArgument(this.paramAnnotated, message);
|
||||
assertEquals("bar",result);
|
||||
result = this.resolver.resolveArgument(this.paramAnnotatedValue, message);
|
||||
assertEquals("value",result);
|
||||
}
|
||||
|
||||
@Test(expected = MessageHandlingException.class)
|
||||
public void resolveArgumentNotFound() throws Exception {
|
||||
Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).build();
|
||||
this.resolver.resolveArgument(this.paramAnnotated, message);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void handleMessage(
|
||||
@PathVariable String foo,
|
||||
@PathVariable(value = "name") String param1,
|
||||
String param3) {
|
||||
}
|
||||
}
|
||||
@@ -28,9 +28,12 @@ import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.handler.annotation.Header;
|
||||
import org.springframework.messaging.handler.annotation.Headers;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.PathVariable;
|
||||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
||||
import org.springframework.messaging.simp.SimpMessageSendingOperations;
|
||||
import org.springframework.messaging.simp.SimpMessageType;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.messaging.simp.annotation.SubscribeEvent;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
||||
@@ -40,6 +43,7 @@ import static org.junit.Assert.*;
|
||||
/**
|
||||
* Test fixture for {@link AnnotationMethodMessageHandler}.
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
*/
|
||||
public class AnnotationMethodMessageHandlerTests {
|
||||
|
||||
@@ -80,6 +84,63 @@ public class AnnotationMethodMessageHandlerTests {
|
||||
this.messageHandler.registerHandler(new DuplicateMappingController());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void messageMappingPathVariableResolution() {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
|
||||
headers.setDestination("/message/bar/value");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
this.messageHandler.handleMessage(message);
|
||||
|
||||
assertEquals("messageMappingPathVariable", this.testController.method);
|
||||
assertEquals("bar", this.testController.arguments.get("foo"));
|
||||
assertEquals("value", this.testController.arguments.get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subscribeEventPathVariableResolution() {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
|
||||
headers.setDestination("/sub/bar/value");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0])
|
||||
.copyHeaders(headers.toMap()).build();
|
||||
this.messageHandler.handleMessage(message);
|
||||
|
||||
assertEquals("subscribeEventPathVariable", this.testController.method);
|
||||
assertEquals("bar", this.testController.arguments.get("foo"));
|
||||
assertEquals("value", this.testController.arguments.get("name"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void antPatchMatchWildcard() {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
|
||||
headers.setDestination("/pathmatch/wildcard/test");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
this.messageHandler.handleMessage(message);
|
||||
|
||||
assertEquals("pathMatchWildcard", this.testController.method);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bestMatchWildcard() {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
|
||||
headers.setDestination("/bestmatch/bar/path");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
this.messageHandler.handleMessage(message);
|
||||
|
||||
assertEquals("bestMatch", this.testController.method);
|
||||
assertEquals("bar", this.testController.arguments.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleBinding() {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
|
||||
headers.setDestination("/binding/id/12");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
this.messageHandler.handleMessage(message);
|
||||
|
||||
assertEquals("simpleBinding", this.testController.method);
|
||||
assertTrue("should be bound to type long", this.testController.arguments.get("id") instanceof Long);
|
||||
assertEquals(12L, this.testController.arguments.get("id"));
|
||||
}
|
||||
|
||||
private static class TestAnnotationMethodMessageHandler extends AnnotationMethodMessageHandler {
|
||||
|
||||
@@ -109,6 +170,44 @@ public class AnnotationMethodMessageHandlerTests {
|
||||
this.arguments.put("foo", foo);
|
||||
this.arguments.put("headers", headers);
|
||||
}
|
||||
|
||||
@MessageMapping("/message/{foo}/{name}")
|
||||
public void messageMappingPathVariable(@PathVariable("foo") String param1,
|
||||
@PathVariable("name") String param2) {
|
||||
this.method = "messageMappingPathVariable";
|
||||
this.arguments.put("foo", param1);
|
||||
this.arguments.put("name", param2);
|
||||
}
|
||||
|
||||
@SubscribeEvent("/sub/{foo}/{name}")
|
||||
public void subscribeEventPathVariable(@PathVariable("foo") String param1,
|
||||
@PathVariable("name") String param2) {
|
||||
this.method = "subscribeEventPathVariable";
|
||||
this.arguments.put("foo", param1);
|
||||
this.arguments.put("name", param2);
|
||||
}
|
||||
|
||||
@MessageMapping("/pathmatch/wildcard/**")
|
||||
public void pathMatchWildcard() {
|
||||
this.method = "pathMatchWildcard";
|
||||
}
|
||||
|
||||
@MessageMapping("/bestmatch/{foo}/path")
|
||||
public void bestMatch(@PathVariable("foo") String param1) {
|
||||
this.method = "bestMatch";
|
||||
this.arguments.put("foo", param1);
|
||||
}
|
||||
|
||||
@MessageMapping("/bestmatch/**")
|
||||
public void otherMatch() {
|
||||
this.method = "otherMatch";
|
||||
}
|
||||
|
||||
@MessageMapping("/binding/id/{id}")
|
||||
public void simpleBinding(@PathVariable("id") Long id) {
|
||||
this.method = "simpleBinding";
|
||||
this.arguments.put("id", id);
|
||||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
|
||||
Reference in New Issue
Block a user