简体   繁体   English

我应该如何正确使用 @GetMapping 来创建 /something/{id}/something/{id2} 路径?

[英]how should I use @GetMapping properly to create a /something/{id}/something/{id2} path?

I've been stuck to this problem for days.我已经被这个问题困扰了好几天了。 I'm creating a website where chefs can put their buffets, their dishes etc.我正在创建一个网站,厨师可以在其中放置自助餐、菜肴等。

This is the code I'm using to get a chef in particular:这是我用来找厨师的代码:

//cs is the chefService
@GetMapping("/chef/{id}")
   public String getChef(@PathVariable("id")Long id, Model model) {

       Chef chef = cs.findById(id);
       model.addAttribute("chef",chef);
       return "chef.html";

(It basically return a page with his buffets) (它基本上返回一个带有他的自助餐的页面)

Now I dont know how to do the same thing for his buffets, precisely i dont know if i have to repeat the id part both for the chef and the buffet or just the buffet id or it's something i need to do in my thymeleaf code.现在我不知道如何为他的自助餐做同样的事情,确切地说,我不知道我是否必须为厨师和自助餐重复 id 部分,或者只是自助餐 id,或者这是我需要在我的百里香代码中做的事情。 Is it correct to do it this way?这样做是否正确? :

   @GetMapping("/chef/{idChef}/buffet/{idBuffet}") 
   public String getBuffetsOf(@PathVariable("idBuffet") Long id, Model model) {
       Buffet buffet = bs.findById(id);
       model.addAttribute("buffet", buffet);
       return "buffet.html";
   }

Or maybe i need to add another Long variable for the chef id?或者也许我需要为厨师 ID 添加另一个 Long 变量? Thanks for the help谢谢您的帮助

EDIT: Here are the two entities:编辑:这是两个实体:

@Entity
public class Chef{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String nome;
    private String cognome;
    private String nazionalita;
    @OneToMany
    @JoinTable(name = "chef_buffet")
    private List<Buffet> buffets;


    public Chef(String nome, String cognome, String nazionalita, List<Buffet> buffets) {
        this.nome = nome;
        this.cognome = cognome;
        this.nazionalita = nazionalita;
        this.buffets = buffets;
    }

    public Chef() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCognome() {
        return cognome;
    }

    public void setCognome(String cognome) {
        this.cognome = cognome;
    }

    public String getNazionalita() {
        return nazionalita;
    }

    public void setNazionalita(String nazionalita) {
        this.nazionalita = nazionalita;
    }

    public List<Buffet> getBuffets() {
        return buffets;
    }

    public void setBuffets(List<Buffet> buffets) {
        this.buffets = buffets;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Chef other = (Chef) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Chef [id=" + id + ", nome=" + nome + ", cognome=" + cognome + ", nazionalita=" + nazionalita
                + ", buffets=" + buffets + "]";
    }

@Entity
public class Buffet {
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    private String nome;
    private String descrizione;
    private Chef chef;
    @OneToMany
    @JoinTable(name = "buffet_piatti")
    private List<Piatti> piattis;


    public Buffet(String nome, String descrizione, List<Piatti> piattis, Chef chef) {
        this.nome = nome;
        this.descrizione = descrizione;
        this.chef = chef;
        this.piattis = piattis;
    }

    public Buffet() {

    }

    public String getNome() {
        return nome;
    }


    public void setNome(String nome) {
        this.nome = nome;
    }


    public String getDescrizione() {
        return descrizione;
    }


    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }


    public List<Piatti> getPiattis() {
        return piattis;
    }


    public void setPiattis(List<Piatti> piattis) {
        this.piattis = piattis;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }
    
    public Chef getChef() {
        return chef;
    }

    public void setChef(Chef chef) {
        this.chef = chef;
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Buffet other = (Buffet) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Buffet [id=" + id + ", nome=" + nome + ", descrizione=" + descrizione + ", piattis=" + piattis + "]";
    }

Since the chef page displays buffets, it makes me believe that you have one-to-many relationship of chef to buffets.由于厨师页面显示自助餐,这让我相信您的厨师与自助餐之间存在一对多的关系。 When displaying all buffets in chef page, you can build the url for each single buffet in thymeleaf.在厨师页面中显示所有自助餐时,您可以在 thymeleaf 中为每个自助餐建立 url。 Something like:就像是:

/chef/buffet/${buffet.id}

Then your mapping for single buffet will be:那么您的单人自助餐映射将是:

@GetMapping("/chef/buffet/{idBuffet}") 
public String getBuffet(@PathVariable("idBuffet") Long id, Model model) {
    Buffet buffet = bs.findById(id);
    model.addAttribute("buffet", buffet);
    return "buffet.html";
}

If you need both chef id and buffet id in the method, you can map both path variables.如果您在方法中同时需要厨师 ID 和自助餐 ID,则可以映射这两个路径变量。

@GetMapping("/chef/{idChef}/buffet/{idBuffet}") 
public String getBuffetsOf(@PathVariable("idChef") Long idChef,
       @PathVariable("idBuffet") Long idBuffet, Model model) {
       //method code here
   }

Looking at your buffets endpoint,看着你的自助餐端点,

@GetMapping("/chef/{idChef}/buffet/{idBuffet}") 
   public String getBuffetsOf(@PathVariable("idBuffet") Long id, Model model) {
       Buffet buffet = bs.findById(id);
       model.addAttribute("buffet", buffet);
       return "buffet.html";
}

You might have missed the {idBuffet} parameter.您可能错过了 {idBuffet} 参数。

Correct code should be,正确的代码应该是,

@GetMapping("/chef/{idChef}/buffet/{idBuffet}") 
   public String getBuffetsOf(@PathVariable("idChef") Long idChef, 
     @PathVariable("idBuffet") Long id, Model model) {
       Buffet buffet = bs.findById(id);
       model.addAttribute("buffet", buffet);
       return "buffet.html";
}

This should work.这应该有效。

Also, if you are not using {idChef} in your method why would you even need that?另外,如果您没有在方法中使用 {idChef} 为什么还需要它? You can just create endpoint without that.您可以在没有它的情况下创建端点。

Something like,就像是,

@GetMapping("/chef/buffet/{id}") 
   public String getBuffetsOf(@PathVariable("id") Long id, Model model) {
       Buffet buffet = bs.findById(id);
       model.addAttribute("buffet", buffet);
       return "buffet.html";
}

OR或者

@GetMapping("/buffet/{id}")
   public String getBuffetsOf(@PathVariable("id") Long id, Model model) {
       Buffet buffet = bs.findById(id);
       model.addAttribute("buffet", buffet);
       return "buffet.html";
}

This will look a lot cleaner.这看起来会干净很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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