简体   繁体   English

如何指示 Spring REST 方法的 PathVariable?

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

I want to indicate the pathvariable secondpart.我想指出路径变量的第二部分。 Thats not possible because an errormessage shows: Could not determine type for: veranstaltung.Identificationnumber, at table: teilnehmer, for columns: [org.hibernate.mapping.Column(id)]这是不可能的,因为错误消息显示:无法确定类型:veranstaltung.Identificationnumber,在表:teilnehmer,对于列:[org.hibernate.mapping.Column(id)]

What I have to change to use the variable secondpart?我必须改变什么才能使用变量 secondpart? How can I access the variable secondpart in the method of the Controller?如何在 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;
}

}

Your Identificationnumber is not primitive but custom class.您的Identificationnumber不是原始的,而是自定义类。 What you are doing is trying to use Long against your Identificationnumber in teilnehmerById method.您正在做的是尝试在teilnehmerById方法中对您的Identificationnumber使用Long

I would suggest to either change @Id column of Teilnehmer to Long from Identificationnumber or you can still pass Identificationnumber in teilnehmerById method by doing something like below (This is based on consideration that you have implemented your own findById which will convert Identificationnumber to 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;

Based on hibernate error, it looks like you have to use my first option which is change @Id of Teilnehmer to Long .基于休眠错误,您似乎必须使用我的第一个选项, Teilnehmer@Id更改为Long

Edit : just updated code so that you can use composition of String to Long.编辑:刚刚更新了代码,以便您可以使用 String 到 Long 的组合。

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

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