2017-10-09 106 views
0

就像在一個標題中,我試圖找到一種方法來獲得POST資源時自動生成的ID。自動生成的ID而不是null或默認值0

我使用embad h2或hsqldb數據庫。

我已經測試了什麼?

  • 我改變策略身份(我知道AUTO應該是一樣的身份,但無論如何試過)
  • 我改變長隆(GET結果:默認值0更改爲默認爲空)
  • 我readed很多帖子,例如這個答案看起來堅實similar post answer但我覺得我的情況是不同的(我應該真正從POST方法刪除ID?)

這裏是我的代碼:

控制器:

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); 
    } 

} 

服務:

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

    } 

} 

庫:

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

} 

型號:

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; 
    } 
} 

我不知道該怎麼做。請分享你的知識。

最良好的祝願, Degath

回答

1

標識策略AUTO是正確的,你只是缺少保存電話到數據庫phoneRepository.save()這就是爲什麼它沒有生成的ID。上面所做的只是添加到列表中並從列表中恢復。試試這個:

@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); 
     phoneRepository.save(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); 
     phoneRepository.save(phones); 
    } 

}