バリューREST @RequestMappingは、値に '. 'が含まれていると正しく抽出されません.

値に「。」が含まれていると、Spring REST @RequestMappingが誤って抽出する

問題

次のSpringRESTの例を参照してください。「http://localhost:8080/site/google.com」などのリクエストが送信されると、Springは「google」を返します。 Springは「。」をファイル拡張子として扱い、パラメータ値の半分を抽出するように見えます。

SiteController.java

package com.example.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/site")
public class SiteController {

    @RequestMapping(value = "/{domain}", method = RequestMethod.GET)
    public String printWelcome(@PathVariable("domain") String domain,
        ModelMap model) {

        model.addAttribute("domain", domain);
        return "domain";

    }

}

溶液

これを修正するには、@RequestMappingが正規表現をサポートするようにし、「.+」を追加します。これは、何にでも一致することを意味します。 これで、Springは「google.com」を返します。

SiteController.java

package com.example.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/site")
public class SiteController {

    @RequestMapping(value = "/{domain:.+}", method = RequestMethod.GET)
    public String printWelcome(@PathVariable("domain") String domain,
        ModelMap model) {

        model.addAttribute("domain", domain);
        return "domain";

    }

}