Spring REST @RequestMapping extrait correctement si la valeur contient '.'

Spring REST @RequestMapping ne s’extrait pas correctement si la valeur contient ‘.’

Problème

Voir l'exemple Spring REST suivant, si une requête telle que «http://localhost:8080/site/google.com» est soumise, Spring renvoie «google». Ressemble à Spring traite «.» Comme une extension de fichier et extrait la moitié de la valeur du paramètre.

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";

    }

}

Solution

Pour résoudre ce problème, make@RequestMapping prend en charge les expressions régulières, ajoutez «.+», cela signifie correspondre à tout. Maintenant, Spring retournera «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";

    }

}