简体   繁体   中英

Spring Boot - Easier way of GetMapping to the same method with different Paths

currently my Controller looks like this.

@RequestMapping("members")
public class MembersController {

    private ArrayList<Member> memberList = new ArrayList<>();

    @GetMapping("")
    public String index1(Model model) {
        model.addAttribute(memberList);
        return "members/memberIndex";
    }

    @GetMapping("index")
    public String index2(Model model) {
        model.addAttribute(memberList);
        return "members/memberIndex";
    }
}

Is there a easier way to have one index-method for two different paths "localhost:port/members" & "localhost:port/members/index"? Can I annotate two values for one method in general?

Thanks in advance:)

Edit: I have seen it work for @RequestMapping

You can use the value of @GetMapping as follows:

@GetMapping(value = {"/", "/index"})

I fixed it myself immediatly.

@GetMapping({"", "index"})

Didn't notice an array is necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM