[英]Problem creating endpoint with springboot
我的任务是创建端点,逻辑是:
用户根据该nip提供输入 --> nip ( Contractor.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;
}
我不知道您确切需要什么逻辑。 此外,我不确定该代码是否适合您的应用程序的 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.