POST,DELTE,GET操作带有复合键的RestController弹簧启动

时间:2018-07-20 09:20:23

标签: java spring rest

我正在寻找使用 JPA 在控制器类中实现此操作(删除,发布,获取)的正确方法。

这是我的代码,我不确定发布是否正确,但是确实删除会导致问题,因为来自Apache Component的HttpDelete没有用于请求正文的setEntity方法。

DAO:

@Repository
public interface PiecesReparationDAO extends JpaRepository<PiecesReparation, Integer>{
    PiecesReparation findById(PieceRId id);
    PiecesReparation findById_Id_produit(int id);
    PiecesReparation findById_Id_Reparation(int id);
    void deleteById(PieceRId id);
}

模型:

@Entity
public class PiecesReparation {

   @EmbeddedId
   PieceRId id;

   @MapsId("id_produit")
   @ManyToOne(optional = false)
   @JoinColumn(name = "id_produit", referencedColumnName = "id")
   private Produit product;

   @MapsId("id_reparation")
   @ManyToOne(optional = false)
   @JoinColumn(name = "", referencedColumnName = "id")
   private Reparation reparation;



   private Timestamp date_utilisation;

   public PieceRId getPieceRId(){
       return this.id;
   }
   public void setPieceRId(PieceRId id){
       this.id=id;
   }

    public Timestamp getDateUtilisation(){
        return this.date_utilisation;
    }

    public void setDateUtilisation(Timestamp date){
        this.date_utilisation=date;
    }



}

PieceRId类:

    @Embeddable
   public class PieceRId implements Serializable {
   int id_produit;
   int id_reparation;

   //implements equals and hashCode
   public int getIdProduit(){
       return this.id_produit;
   }

   public int getIdReparation(){
       return this.id_reparation;
   }

   public PieceRId(){

   }

   public PieceRId(int id_produit, int id_reparation){
       this.id_produit=id_produit;
       this.id_reparation=id_reparation;
   }

   @Override
   public boolean equals(Object o){
       if(o ==null) return false;
       if(((PieceRId)o).getIdProduit() == this.getIdProduit() &&
               ((PieceRId)o).getIdReparation() == this.getIdReparation())
           return true;
       else return false;
   }

   @Override
   public int hashCode() { 
       return Integer.valueOf(String.valueOf(id_produit)+String.valueOf(id_reparation));
   }
}

控制器:

@RestController
public class PiecesReparationController {
   @Autowired
    private PiecesReparationDAO piecesReparationDao;

    @GetMapping(value = "/Pieces")
    public ArrayList<PiecesReparation> listePiecesReparations() {
        ArrayList<PiecesReparation> listePiecesReparations= new ArrayList() ;
        Iterable<PiecesReparation> piecesReparations = piecesReparationDao.findAll();
         MappingJacksonValue produitsFiltres = new MappingJacksonValue(piecesReparations);
         for (Iterator<PiecesReparation> i = piecesReparations.iterator(); i.hasNext();) {
            PiecesReparation item = i.next();
               listePiecesReparations.add(item);
         }
         return listePiecesReparations;
    }

    //Récupérer un produit par son Id
    @GetMapping(value = "/Pieces/{id}")
    public PiecesReparation selectionPiecesReparation(@PathVariable PieceRId id) {
        return piecesReparationDao.findById(id);
    }

    //ajouter un produit
    @PostMapping(value = "/Pieces")
    public ResponseEntity<?> ajouterPiecesReparation(@RequestBody PieceReparationDTO pieceReparation) {

        PiecesReparation pieceR = new PiecesReparation();
        pieceR.setPieceRId(new PieceRId(pieceReparation.getIdProduit(), pieceReparation.getIdReparation()));
        pieceR.setDateUtilisation(pieceReparation.getDateUtilisation());
        PiecesReparation piecesReparationAdded =  piecesReparationDao.save(pieceR);

        if (piecesReparationAdded == null)
            return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
        else return new ResponseEntity<>(HttpStatus.CREATED);

    }

   @DeleteMapping(value = "/Pieces/{id}")
   public void supprimerPiecesReparation(@PathVariable PieceRId id) {

       piecesReparationDao.delete(id);
   }

   @DeleteMapping(value = "/Pieces")
   public void supprimerPiecesReparation2(@RequestBody PieceRId id) {
       PiecesReparation pieceR= new PiecesReparation();
       pieceR.setPieceRId(new PieceRId(id.getIdProduit(),id.getIdReparation()));
       piecesReparationDao.delete(pieceR);
   }




  @PutMapping (value = "/Pieces")
  public void updateProduit(@RequestBody PieceReparationDTO pieceReparation) {
       PiecesReparation pieceR = new PiecesReparation();
        pieceR.setPieceRId(new PieceRId(pieceReparation.getIdProduit(), pieceReparation.getIdReparation()));
        pieceR.setDateUtilisation(pieceReparation.getDateUtilisation());
        PiecesReparation piecesReparationAdded =  piecesReparationDao.save(pieceR);
  } 
}

使用网络服务的方法:

public static void deleteRest(int id, String type){

        try{
        CloseableHttpClient client = HttpClientBuilder.create().build();  

            HttpDelete deleteRequest = new HttpDelete(getUrl(type)+"/"+id);
            HttpResponse response=client.execute(deleteRequest);
            System.out.print("response code : 
 "+response.getStatusLine().getStatusCode());
            client.close();
        }catch (ClientProtocolException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();
    } 



    }

我知道如何使用url和path变量删除,但不处理请求正文,因为没有setEntity方法。如果执行正确,我不确定其他操作是否可以。谢谢。

0 个答案:

没有答案