RequestMapping自定义属性

RequestMapping自定义属性

本文介绍了如何实现@RequestMapping自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为示例,请执行子域映射

本文:
建议解析Filter上的子域并将变量赋值给ServletRequest头。

This article: Managing multiple Domain and Sub Domain on Google App Engine for Same Applicationrecommends to resolve subdomain on Filter and assign variable to ServletRequest headers.

然后映射将如下所示:

@RequestMapping(value = "/path", headers="subdomain=www")
 public String subsiteIndexPage(Model model,HttpServletRequest request) { ... }

如果我们想创建自定义@RequestMapping属性,例如子域,例如。创建这样的映射:

If we'd like to create custom @RequestMapping property, such as subdomain, eg. to create mapping like this:

@RequestMapping(value = "/some/action", subdomain = "www")
public String handlerFunction(){ ... }

我们应该覆盖 @RequestMapping @interface 定义和覆盖RequestMappingHandlerMapping 受保护的方法,使用我们自己的实现

(如JIRA所述:。

we should override @RequestMapping @interface definition and override RequestMappingHandlerMapping protected methods, with our own implementation
(as stated on JIRA: "Allow custom request mapping conditions SPR-7812").

是不是?任何人都可以提供一个提示,如何实现这个功能?

Is it right? Can anybody provide a hint, how to achieve this functionality?

创意1

正如原始jira线程所建议的那样,是创建自己的 RequestCondition

有一个在github上使用此解决方案的项目:

There is an project which uses this solution available on github: https://github.com/rstoyanchev/spring-mvc-31-demo/

以及相关的SO问题:

And related SO question: Adding custom RequestCondition's in Spring mvc 3.1

可能映射如类型和方法的@Subdomain(www)是可能的解决方案吗?

Maybe mapping like @Subdomain("www") for both Type and Method, is possible solution?

推荐答案

我基于引用的

此解决方案使用Spring 3.1.1.RELEASE平台的自定义@RequestCondition功能

USAGE

示例1:

@Controller
@SubdomainMapping(value = "subdomain", tld = ".mydomain.com")
class MyController1 {
    // Code here will be executed only on address match:
    // subdomain.mydomain.com
}

示例2:

@Controller
class MyController2 {

    @RequestMapping("/index.html")
    @SubdomainMapping("www")
    public function index_www(Map<Object, String> map){
        // on www.domain.com
        // where ".domain.com" is defined in SubdomainMapping.java
    }

    @RequestMapping("/index.html")
    @SubdomainMapping("custom")
    public function index_custom(Map<Object, String> map){
        // on custom.domain.com
        // where ".domain.com" is defined in SubdomainMapping.java
    }
}

我们需要三个文件


  • SubdomainMapping.java

  • SubdomainRequestCondition.java

  • SubdomainRequestMappingHandlerMapping.java

SubdomainMapping.java

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SubdomainMapping {

    /**
    * This param defines single or multiple subdomain
    * Where the Method/Type is valid to be called
    */
    String[] value() default {};
    /**
    * This param defines site domain and tld
    * It's important to put the leading dot
    * Not an array, so cannot be used for mapping multiple domains/tld
    */
    String tld() default ".custom.tld";
}

SubdomainRequestCondition.java

import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.mvc.condition.RequestCondition;

public class SubdomainRequestCondition implements
        RequestCondition<SubdomainRequestCondition> {

    private final Set<String> subdomains;
    private final String tld;

    public SubdomainRequestCondition(String tld, String... subdomains) {
        this(tld, Arrays.asList(subdomains));
    }

    public SubdomainRequestCondition(String tld, Collection<String> subdomains) {
        this.subdomains = Collections.unmodifiableSet(new HashSet<String>(
                subdomains));
        this.tld = tld;
    }

    @Override
    public SubdomainRequestCondition combine(SubdomainRequestCondition other) {
        Set<String> allRoles = new LinkedHashSet<String>(this.subdomains);
        allRoles.addAll(other.subdomains);
        return new SubdomainRequestCondition(tld, allRoles);
    }

    @Override
    public SubdomainRequestCondition getMatchingCondition(
            HttpServletRequest request) {
        try {
            URL uri = new URL(request.getRequestURL().toString());
            String[] parts = uri.getHost().split(this.tld);
            if (parts.length == 1) {
                for (String s : this.subdomains) {
                    if (s.equalsIgnoreCase(parts[0])) {
                        return this;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
        return null;
    }

    @Override
    public int compareTo(SubdomainRequestCondition other,
            HttpServletRequest request) {
        return org.apache.commons.collections.CollectionUtils.removeAll(other.subdomains, this.subdomains).size();
    }

}

SubdomainRequestMappingHandlerMapping.java

import java.lang.reflect.Method;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

public class CustomRequestMappingHandlerMapping extends
        RequestMappingHandlerMapping {

    @Override
    protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
        SubdomainMapping typeAnnotation = AnnotationUtils.findAnnotation(
                handlerType, SubdomainMapping.class);
        return createCondition(typeAnnotation);
    }

    @Override
    protected RequestCondition<?> getCustomMethodCondition(Method method) {
        SubdomainMapping methodAnnotation = AnnotationUtils.findAnnotation(
                method, SubdomainMapping.class);
        return createCondition(methodAnnotation);
    }

    private RequestCondition<?> createCondition(SubdomainMapping accessMapping) {
        return (accessMapping != null) ? new SubdomainRequestCondition(
                accessMapping.tld(), accessMapping.value()) : null;
    }

}

Instalation




  • 你必须注册自定义MappingHandler bean,指向这个自定义实现 SubdomainRequestMappingHandlerMapping class

  • 您必须将其顺序设置为低于默认值 RequestMappingHandlerMapping

    OR

    替换已注册的 RequestMappingHandlerMapping (可能在订单= 0)

    • You have to register custom MappingHandler bean, pointing at this custom implementation SubdomainRequestMappingHandlerMapping class
    • You have to set it's order to be lower than default RequestMappingHandlerMapping
      OR
      Replace the registered RequestMappingHandlerMapping (possibly on order=0)
    • 有关实施此解决方案的更多解释,请参阅相关的github项目

      这篇关于如何实现@RequestMapping自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:35