Comme dans un titre, j'essaie de trouver un moyen d'obtenir l'ID auto-généifié lors de la ressource POST. J'utilise une base de données h2 ou hsqldb embarquée.Id généré automatiquement au lieu de null ou par défaut 0
Ce que je testais déjà?
- j'ai changé la stratégie d'identité (je sais que AUTO doit être la même que l'identité, mais ont essayé de toute façon)
- j'ai changé longtemps pour Long (GET résultat: par défaut 0 changé pour null par défaut)
- I readed beaucoup de messages par exemple cette réponse semble solide similar post answer mais je pense que mon cas est différent (dois-je supprimer vraiment id de la méthode POST?)
Voici mon code:
Controller:
package spring.degath.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spring.degath.data.PhoneRepository;
import spring.degath.model.Phone;
import spring.degath.services.PhoneService;
import java.util.List;
@RestController
public class PhoneController {
@Autowired
private PhoneService phoneService;
@Autowired
private PhoneRepository phoneRepository;
@RequestMapping(method = RequestMethod.GET, value = "/phones")
public List<Phone> getAllPhones(){
return phoneService.getAllPhones();
}
@RequestMapping(method = RequestMethod.GET, value = "/phones/{id}")
public Phone getPhone(@PathVariable Long id){
return phoneService.getPhone(id);
}
@RequestMapping(method = RequestMethod.POST, value = "/phones")
public void addPhone(@RequestBody Phone phone){
phoneService.addPhone(phone);
}
}
Service:
package spring.degath.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import spring.degath.data.PhoneRepository;
import spring.degath.model.Phone;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
@Service
public class PhoneService {
private List<Phone> phones;
@Autowired
private PhoneRepository phoneRepository;
public List<Phone> getAllPhones(){
return phones;
}
public Phone getPhone(final Long id){
return phoneRepository.findById(id);
}
public void addPhone(Phone phone) {
phones.add(phone);
}
@PostConstruct
private void initDataForTesting() {
phones = new ArrayList<Phone>();
Phone phone1 = new Phone((long) 1,"nokia");
Phone phone2 = new Phone((long) 2,"sony");
Phone phone3 = new Phone((long) 3,"samsung");
phones.add(phone1);
phones.add(phone2);
phones.add(phone3);
}
}
Repository:
package spring.degath.data;
import org.springframework.data.repository.CrudRepository;
import spring.degath.model.Phone;
public interface PhoneRepository extends CrudRepository<Phone, String> {
Phone findById(Long id);
}
Modèle:
package spring.degath.model;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
@Entity
public class Phone {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@NotNull
private Long id;
private String brand;
public Phone(){
}
public Phone(Long id, String brand) {
super();
this.id = id;
this.brand = brand;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
Je n'ai pas la moindre idée de quoi faire. S'il vous plaît partagez vos connaissances.
Meilleurs voeux, Degath