简体   繁体   中英

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)]

What I have to change to use the variable secondpart? How can I access the variable secondpart in the method of the Controller?

    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. What you are doing is trying to use Long against your Identificationnumber in teilnehmerById method.

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 ):

@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 .

Edit : just updated code so that you can use composition of String to Long.

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