簡體   English   中英

使用 springboot 創建端點的問題

[英]Problem creating endpoint with springboot

我的任務是創建端點,邏輯是:
用戶根據該nip提供輸入 --> nipContractor.class中的變量之一),我必須返回 JSON,其中將包含有關分配給具有提供的nip的承包商的產品的信息。 示例 JSON 應如下所示: { "name": "product_name", "quantity": "0", "address": "storage_address"}

我在這個問題上花了很多時間,但仍然不知道要實現什么邏輯。 它在我的新手頭上;

產品.class :

public class Product extends AbstractEntity {


    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private long quantity;


    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "product", fetch = FetchType.EAGER)
    private List<Assignment> assignments;


    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "product")
    private List<Place> places;
}

承包商.class :

public class Contractor extends AbstractEntity {

    @Id
    @GeneratedValue
    private Long id;
    private String contractorName;
    private int nip;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "contractor")
    private List<Assignment> assignments;
}

分配.class

public class Assignment extends AbstractEntity {

    @Id
    @GeneratedValue
    private Long id;


    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "id_product", referencedColumnName = "id", nullable = false)
    private Product product;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "id_contractor", referencedColumnName = "id", nullable = false)
    private Contractor contractor;

}

存儲.class

public class Storage extends AbstractEntity {

    @Id
    @GeneratedValue
    private Long id;
    private String address;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "storage")
    private List<Place> places;
}

Place.class

public class Place extends AbstractEntity {

    @Id
    @GeneratedValue
    private Long id;
    private Long shelfNumber;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "id_product", referencedColumnName = "id")
    private Product product;


    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "id_storage", referencedColumnName = "id", nullable = false)
    private Storage storage;

}

ERD 圖的圖像

我不知道您確切需要什么邏輯。 此外,我不確定該代碼是否適合您的應用程序的 rest。 但也許它會有所幫助。

將由存儲庫和 rest controller 返回的接口:

public interface GetProductResponse {
    public String getName();
    public int getQuantity();
    public String getAddress();
}

您可以在其中編寫查詢的存儲庫:

public interface ProductRepository extends CrudRepository<Product, Long>  {
    @Query(nativeQuery = true, 
    value = (
"SELECT product.name AS name, product.quantity AS quantity, storage.address " + 
//be sure to name the result columns the variables in GetProductResponse (without the 'get')
"FROM contractor INNER JOIN assignment ON contractor.id = assignment.id_contractor " +
" INNER JOIN product ON product.id = assignment.id " +
" INNER JOIN place ON product.id = place.id_product " +
" INNER JOIN storage ON storage.id = place.id_storage " +
"WHERE contractor.nip = :nip_ "
    )
    public List<GetProductResponse> getProducts(@Param("nip_")String nip)
}

rest controller:

@RestController
public class Controller {
    @RequestMapping(value = "/getProductsByNip", method = { RequestMethod.POST})
    public List<GetProductResponsee> getProductsByNip(@RequestBody String nip) {
        return productRepository.getProducts(nip);
    }
}

output 將如下所示:

[
{"name": "product_name1", "quantity": "0", "address": "storage_address1"},
{"name": "product_name2", "quantity": "2", "address": "storage_address2"}
]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM