简体   繁体   English

使用 springboot 创建端点的问题

[英]Problem creating endpoint with springboot

My task is to create endpoint, the logic is:我的任务是创建端点,逻辑是:
User provides input --> nip (one of the variables in Contractor.class ) on the basis of that nip , I must return JSON, which will contain information about the product that is assigned to the contractor with the provided nip .用户根据该nip提供输入 --> nipContractor.class中的变量之一),我必须返回 JSON,其中将包含有关分配给具有提供的nip的承包商的产品的信息。 Example JSON should look like: { "name": "product_name", "quantity": "0", "address": "storage_address"}示例 JSON 应如下所示: { "name": "product_name", "quantity": "0", "address": "storage_address"}

I spent lots of time on this problem, but still don't know what logic to implement.我在这个问题上花了很多时间,但仍然不知道要实现什么逻辑。 It's over my newbie head;它在我的新手头上;

Product.class :产品.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 :承包商.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 :分配.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 :存储.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 : 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 ERD 图的图像

I do not know what logic do you need precisely.我不知道您确切需要什么逻辑。 Also I am not sure if that code will fit to rest of your app.此外,我不确定该代码是否适合您的应用程序的 rest。 But maybe it will help.但也许它会有所帮助。

The interface which will be returned by repository and by the rest controller:将由存储库和 rest controller 返回的接口:

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

The repository where you can write your query:您可以在其中编写查询的存储库:

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

The rest controller: rest controller:

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

The output will look like: 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