rstoyanchev / spring-mvc-31-demo

Demonstrates Spring MVC 3.1 Specific Customization Options
83 stars 46 forks source link

Custom Request Mapping on Type level #6

Open smarek opened 12 years ago

smarek commented 12 years ago

Can you please modify the provided example of @RoleMapping to provide @Controller with role mapping?

Simply using this, does not work:

@RoleMapping(role = ...)
@Controller
public class MappingTestController{
...
}
smarek commented 12 years ago

OK, now when digging little bit deeper I found this, which means, any of CustomCondition cannot be used separately without defining @RequestMapping on same level (Method, Type).

I think this is bad approach, if that does mean we cannot use eg. RoleMapping on @Controller and @RequestMapping to define path of each method absolutely.

org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.class (https://github.com/SpringSource/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.java)

    /**
     * Uses method and type-level @{@link RequestMapping} annotations to create
     * the RequestMappingInfo.
     * 
     * @return the created RequestMappingInfo, or {@code null} if the method
     * does not have a {@code @RequestMapping} annotation.
     * 
     * @see #getCustomMethodCondition(Method)
     * @see #getCustomTypeCondition(Class)
     */
    @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
        RequestMappingInfo info = null;
        RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
        if (methodAnnotation != null) {
            RequestCondition<?> methodCondition = getCustomMethodCondition(method);
            info = createRequestMappingInfo(methodAnnotation, methodCondition);
            RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
            if (typeAnnotation != null) {
                RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType);
                info = createRequestMappingInfo(typeAnnotation, typeCondition).combine(info);
            }
        }
        return info;
    }
smarek commented 12 years ago

The proper workaround now is to use empty @RequestMapping, but I wonder if this is the nice way to do it:

example

@Controller
@PreAuthorize("hasAuthority('ROLE_MANAGER')")
@RequestMapping
@SubdomainMapping(value = { "managers", "www" })
public class ManagerController {