使用 spring boot 创建端点时出现问题

Problem creating endpoint with springboot

我的任务是创建端点,逻辑是:
用户提供输入 --> nipContractor.class中的变量之一) 基于 nip,我必须 return JSON,其中将包含有关分配给承包商的产品的信息,并提供 nip。 示例 JSON 应如下所示:{"name": "product_name", "quantity": "0", "地址": "storage_address"}

我在这个问题上花了很多时间,但仍然不知道要实现什么逻辑。 这是我的新手头脑;

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

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

Assignment.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;

}

Storage.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;

}

image of ERD Diagram

不知道你具体需要什么逻辑。此外,我不确定该代码是否适合您应用程序的其余部分。但也许它会有所帮助。

存储库和其余控制器将返回的接口:

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

其余控制器:

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

输出将如下所示:

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