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?

/

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Even if technically not the right answer, it was useful to me in my situation. I'm glad it was here.
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);
    }    
}

1 Comment

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}.
1

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.