簡體   English   中英

如何指示 Spring REST 方法的 PathVariable?

[英]How can I indicate the PathVariable for Spring REST-method?

我想指出路徑變量的第二部分。 這是不可能的,因為錯誤消息顯示:無法確定類型:veranstaltung.Identificationnumber,在表:teilnehmer,對於列:[org.hibernate.mapping.Column(id)]

我必須改變什么才能使用變量 secondpart? 如何在 Controller 的方法中訪問變量 secondpart?

    package veranstaltung;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Teilnehmer {


static int idnumber=69;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Identificationnumber id;
private String name;
private String vorname;
private String wohnort;

protected Teilnehmer() {}

public Teilnehmer(Identificationnumber id, String name, String vorname,String wohnort)
{
    this.id=id;
    this.name=name;
    this.vorname=vorname;
    this.wohnort=wohnort;
}


public static String erzeugeID ()
{
    String id= "JAVALAND-";
    id=id+idnumber;
    idnumber++;
    return id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getVorname() {
    return vorname;
}

public void setVorname(String vorname) {
    this.vorname = vorname;
}

public String getWohnort() {
    return wohnort;
}

public void setWohnort(String wohnort) {
    this.wohnort = wohnort;
}


@Override
public String toString()
{
    return  id.getfullID()+" "+getName()+" "+getVorname()+" "+getWohnort();
}

}

    package veranstaltung;

public class Identificationnumber {


private String firstpart;
private Long secondpart;

public Identificationnumber(String firstpart, Long secondpart)
{
    this.firstpart=firstpart;
    this.secondpart=secondpart;
}


public String getFirstpart() {
    return firstpart;
}

public void setFirstpart(String firstpart) {
    this.firstpart = firstpart;
}

public Long getSecondpart() {
    return secondpart;
}

public void setSecondpart(Long secondpart) {
    this.secondpart = secondpart;
}

public String getfullID()
{
    return firstpart+' '+secondpart;
}

}

    package veranstaltung;

import veranstaltung.Teilnehmer;
import veranstaltung.Identificationnumber;
import veranstaltung.TeilnehmerRepository;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class TeilnehmerController {

@Autowired
TeilnehmerRepository teilnehmerRepository;


@GetMapping("/teilnehmer")
    Iterable<Teilnehmer> teilnehmer(){
        return this.teilnehmerRepository.findAll();
    }

@GetMapping("/teilnehmer/{id}")
Teilnehmer teilnehmerById(@PathVariable Long secondpart){
    Optional<Teilnehmer> teilnehmerOptional = this.teilnehmerRepository.findById(secondpart);
    if(teilnehmerOptional.isPresent()) {
        return teilnehmerOptional.get();
    }
    return null;
}

}

您的Identificationnumber不是原始的,而是自定義類。 您正在做的是嘗試在teilnehmerById方法中對您的Identificationnumber使用Long

我建議將Teilnehmer@Id列從Identificationnumber更改為Long ,或者您仍然可以通過執行以下操作在teilnehmerById方法中傳遞Identificationnumber (這是基於考慮到您已經實現了自己的findById ,它將將Identificationnumber轉換為Long ):

@GetMapping("/teilnehmer/{id}")
Teilnehmer teilnehmerById(@PathVariable Long secondpart){
     Identificationnumber number = new Identificationnumber();
     number.setSecondpart(secondpart);
   
  
    Optional<Teilnehmer> teilnehmerOptional = this.teilnehmerRepository.findById(number.getfullID());
    if(teilnehmerOptional.isPresent()) {
        return teilnehmerOptional.get();
    }
    return null;

基於休眠錯誤,您似乎必須使用我的第一個選項, Teilnehmer@Id更改為Long

編輯:剛剛更新了代碼,以便您可以使用 String 到 Long 的組合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM