001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.messaging.handler.annotation; 018 019import java.lang.annotation.Documented; 020import java.lang.annotation.ElementType; 021import java.lang.annotation.Retention; 022import java.lang.annotation.RetentionPolicy; 023import java.lang.annotation.Target; 024 025import org.springframework.messaging.Message; 026 027/** 028 * Annotation for mapping a {@link Message} onto message-handling methods by matching 029 * to the message destination. This annotation can also be used on the type-level in 030 * which case it defines a common destination prefix or pattern for all method-level 031 * annotations including method-level 032 * {@link org.springframework.messaging.simp.annotation.SubscribeMapping @SubscribeMapping} 033 * annotations. 034 * 035 * <p>Handler methods which are annotated with this annotation are allowed to have 036 * flexible signatures. They may have arguments of the following types, in arbitrary 037 * order: 038 * <ul> 039 * <li>{@link Message} to get access to the complete message being processed.</li> 040 * <li>{@link Payload}-annotated method arguments to extract the payload of 041 * a message and optionally convert it using a 042 * {@link org.springframework.messaging.converter.MessageConverter}. 043 * The presence of the annotation is not required since it is assumed by default 044 * for method arguments that are not annotated. Payload method arguments annotated 045 * with Validation annotations (like 046 * {@link org.springframework.validation.annotation.Validated}) will be subject to 047 * JSR-303 validation.</li> 048 * <li>{@link Header}-annotated method arguments to extract a specific 049 * header value along with type conversion with a 050 * {@link org.springframework.core.convert.converter.Converter} if necessary.</li> 051 * <li>{@link Headers}-annotated argument that must also be assignable to 052 * {@link java.util.Map} for getting access to all headers.</li> 053 * <li>{@link org.springframework.messaging.MessageHeaders} arguments for 054 * getting access to all headers.</li> 055 * <li>{@link org.springframework.messaging.support.MessageHeaderAccessor} or 056 * with STOMP over WebSocket support also sub-classes such as 057 * {@link org.springframework.messaging.simp.SimpMessageHeaderAccessor} 058 * for convenient access to all method arguments.</li> 059 * <li>{@link DestinationVariable}-annotated arguments for access to template 060 * variable values extracted from the message destination (e.g. /hotels/{hotel}). 061 * Variable values will be converted to the declared method argument type.</li> 062 * <li>{@link java.security.Principal} method arguments are supported with 063 * STOMP over WebSocket messages. It reflects the user logged in to the 064 * WebSocket session on which the message was received. Regular HTTP-based 065 * authentication (e.g. Spring Security based) can be used to secure the 066 * HTTP handshake that initiates WebSocket sessions.</li> 067 * </ul> 068 * 069 * <p>A return value will get wrapped as a message and sent to a default response 070 * destination or to a custom destination specified with an {@link SendTo @SendTo} 071 * method-level annotation. Such a response may also be provided asynchronously 072 * via a {@link org.springframework.util.concurrent.ListenableFuture} return type 073 * or a corresponding JDK 8 {@link java.util.concurrent.CompletableFuture} / 074 * {@link java.util.concurrent.CompletionStage} handle. 075 * 076 * <h3>STOMP over WebSocket</h3> 077 * <p>An {@link SendTo @SendTo} annotation is not strictly required — by default 078 * the message will be sent to the same destination as the incoming message but with 079 * an additional prefix ({@code "/topic"} by default). It is also possible to use the 080 * {@link org.springframework.messaging.simp.annotation.SendToUser} annotation to 081 * have the message directed to a specific user if connected. The return value is 082 * converted with a {@link org.springframework.messaging.converter.MessageConverter}. 083 * 084 * <p><b>NOTE:</b> When using controller interfaces (e.g. for AOP proxying), 085 * make sure to consistently put <i>all</i> your mapping annotations - such as 086 * {@code @MessageMapping} and {@code @SubscribeMapping} - on 087 * the controller <i>interface</i> rather than on the implementation class. 088 * 089 * @author Rossen Stoyanchev 090 * @since 4.0 091 * @see org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler 092 */ 093@Target({ElementType.TYPE, ElementType.METHOD}) 094@Retention(RetentionPolicy.RUNTIME) 095@Documented 096public @interface MessageMapping { 097 098 /** 099 * Destination-based mapping expressed by this annotation. 100 * <p>For STOMP over WebSocket messages: this is the destination of the 101 * STOMP message (e.g. {@code "/positions"}). Ant-style path patterns 102 * (e.g. {@code "/price.stock.*"}) and path template variables (e.g. 103 * <code>"/price.stock.{ticker}"</code>) are also supported. 104 */ 105 String[] value() default {}; 106 107}