3

I've written following code:

@Controller
@RequestMapping("/page{number}")
public class IndexController
{ 
    @RequestMapping(method = RequestMethod.GET)
    public String printIndex(ModelMap model, @PathVariable int number)
    {
        String numberText;

        switch (number)
        {
            case 0:
                numberText = "Zero";
                break;
            case 1:
                numberText = "One";
                break;
            default:
                numberText = "Unknown";
                break;
        }

        model.addAttribute("number", numberText);

        return "page";
    }
}

And I'm tring to achieve URLs like page1.html, page2.html, page3.html controlled by this method, with one exception: page.html should give same result as page1.html. I'm looking for something to make {number} optional, now it's required.

Is there a way to do that at I said?

/

Adrian Adamczyk
  • 3,000
  • 5
  • 25
  • 41
  • possible duplicate of [With SPRING 3.0, can I make an optional path variable](http://stackoverflow.com/questions/4904092/with-spring-3-0-can-i-make-an-optional-path-variable) – Grzegorz Rożniecki Sep 10 '12 at 20:57

4 Answers4

15

You can use something like this:

@RequestParam(value = "name", defaultValue = "") Long name

Keep in mind that you must use the wrappers (like Long) and not the primitives ones (like long).

I hope this will be useful.

lmcanavals
  • 2,339
  • 1
  • 24
  • 35
Santiago
  • 159
  • 1
  • 2
  • But how does this map to a URL parameter? This would require one to use *a query parameter* such as `/page?number=123` rather than a URL path parameter like `/page123` as asked about in the question. – Arjan Mar 25 '13 at 10:41
  • 2
    Even if technically not the right answer, it was useful to me in my situation. I'm glad it was here. – Kimball Robinson Oct 29 '13 at 17:26
7

How about this:

@Controller
public class IndexController
{ 
    @RequestMapping("/page{number}")
    public String printIndex(ModelMap model, @PathVariable("number") int number)
    {
        String numberText;

        switch (number)
        {
            case 0:
                numberText = "Zero";
                break;
            case 1:
                numberText = "One";
                break;
            default:
                numberText = "Unknown";
                break;
        }

        model.addAttribute("number", numberText);

        return "page";
    }
    @RequestMapping("/page")
    public String printIndex(ModelMap model)
    {
        return printIndex(model, 1);
    }    
}
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • I had same idea, but I thought that Spring might handle this problem almost itself. I don't know how to explain this, let's say I was expecting something like {number, default=1}. – Adrian Adamczyk Sep 10 '12 at 21:00
1

you might want to implement a custom WebArgumentResolver and annotation @OptionalPathVariable and handle it yourself

marco.eig
  • 4,209
  • 2
  • 18
  • 26
0

Rest API - Optional Parameters and change values

@GetMapping(value = "/country/list")
public ResponseEntity<?> companyInformationList
(
Pageable pageable,
@RequestParam(name = "q", required = false,defaultValue = "") String q,
@RequestParam(name = "sortby", required = false, defaultValue = "companyId") String sortby,
@RequestParam(name = "order", required = false, defaultValue = "desc") String order,
@RequestHeader(value = "Accept-Language", defaultValue = "ar") String lang ) {

        
        if(sortby.equalsIgnoreCase("countryName")) {
            if(lang.equalsIgnoreCase("en")) {
                sortby="countryNameEn"; 
            }else
            if(lang.equalsIgnoreCase("ar")) {
                sortby="countryNameAr";
            }else
            if(lang.equalsIgnoreCase("fr")) {
                sortby="countryNameFr";
        }
Community
  • 1
  • 1