Spring boot 3问题:为什么不能使用Postman和Frontend调用API调用API,然后得到空数组?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring boot 3问题:为什么不能使用Postman和Frontend调用API调用API,然后得到空数组?相关的知识,希望对你有一定的参考价值。
我创建了一个API,它称为搜索商店功能。。
mysql脚本
[当我使用此SQL脚本时,将在MySql中运行。它将显示有效结果
SET @productName = null;
SET @BranchName = null;
SET @minPrice = 0;
SET @maxPrice = 10000;
SET @quantity = 1000;
SELECT * FROM stores s
INNER JOIN products p
ON p.id = s.product_id
INNER JOIN branches b
ON b.id = s.branch_id
WHERE (p.name LIKE CONCAT('%', @productName , '%') OR @productName IS NULL)
AND (b.name LIKE CONCAT('%', @branchName , '%') OR @branchName IS NULL)
AND (@minPrice <= p.price AND p.price <= @maxPrice)
AND s.quantity <= @quantity
ORDER BY p.name, b.name, quantity;
。
点击图片查看MySQL的结果
来自StoreService.java和Hibernate的日志
单击以查看邮递员在调用API前>>
当我通过前端调用它时,发生了StoreService.java中的日志为...
productName: null, minPrice: 0.0, maxPrice: 15000.0, branchName: null, 1000 Hibernate: SELECT * FROM stores s INNER JOIN products p ON p.id = s.product_id INNER JOIN branches b ON b.id = s.branch_id WHERE (p.name LIKE CONCAT('%', ? , '%') OR ? IS NULL) AND (b.name LIKE CONCAT('%', ? , '%') OR ? IS NULL) AND (? <= p.price AND p.price <= ?) AND s.quantity <= ? ORDER BY p.name Stores: []
。
我通过邮递员调用API时的错误日志
Postman Params image邮递员致电时会得到
java.lang.StackOverflowError: null at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:944) ~[mysql-connector-java-8.0.19.jar:8.0.19] at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1003) ~[mysql-connector-java-8.0.19.jar:8.0.19] at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52) ~[HikariCP-3.4.2.jar:na] at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java) ~[HikariCP-3.4.2.jar:na] at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:57) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.getResultSet(AbstractLoadPlanBasedLoader.java:390) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:163) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:104) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:707) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:76) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:108) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2145) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:589) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:264) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:585) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:149) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:458) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at com.g_able.back_g_exam.entity.Product.hashCode(Product.java:12) ~[classes/:na] at com.g_able.back_g_exam.entity.Store.hashCode(Store.java:13) ~[classes/:na] at java.util.HashMap.hash(HashMap.java:339) ~[na:1.8.0_172] at java.util.HashMap.put(HashMap.java:612) ~[na:1.8.0_172] at java.util.HashSet.add(HashSet.java:220) ~[na:1.8.0_172] at java.util.AbstractCollection.addAll(AbstractCollection.java:344) ~[na:1.8.0_172] at org.hibernate.collection.internal.PersistentSet.endRead(PersistentSet.java:355) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:239) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:224) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.engine.loading.internal.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:198) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.endLoading(CollectionReferenceInitializerImpl.java:154) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishLoadingCollections(AbstractRowReader.java:267) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final] at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:218) ~[hibernate-core-5.4.10.Final.jar:5.4.10.Final]
。
我的Springboot代码:StoreController.java
@GetMapping("/api/v1/stores/advance-search") public List<Store> findStoresByFilter( @RequestParam(required = false) String productName, @RequestParam(required = false) String branchName, @RequestParam(required = false, defaultValue = "0") Integer minPrice, @RequestParam(required = false, defaultValue = "999999") Integer maxPrice, @RequestParam(required = false, defaultValue = "3000") Integer quantity) ProductParams productParams = ProductParams.builder().name(productName).minPrice(minPrice).maxPrice(maxPrice).build(); Branch branch = Branch.builder().name(branchName).build(); List<Store> stores = storeService.findStoresByFilter(productParams, branch, quantity); System.out.println("Stores: " + stores); return stores;
。
StoreService.javapublic List<Store> findStoresByFilter(ProductParams productParams, Branch branch, int quantity)
System.out.println("productName: " + productParams.getName() + ", minPrice: " + productParams.getMinPrice() + ", maxPrice: "
+ productParams.getMaxPrice() + ", branchName: " + branch.getName() + ", " + quantity);
return storeRepository.searchStoresByFilter(productParams.getName(), branch.getName(),
productParams.getMinPrice(), productParams.getMaxPrice(), quantity);
public List<Store> findStoresByFilter(ProductParams productParams, Branch branch, int quantity)
System.out.println("productName: " + productParams.getName() + ", minPrice: " + productParams.getMinPrice() + ", maxPrice: "
+ productParams.getMaxPrice() + ", branchName: " + branch.getName() + ", " + quantity);
return storeRepository.searchStoresByFilter(productParams.getName(), branch.getName(),
productParams.getMinPrice(), productParams.getMaxPrice(), quantity);
。
StoreRepository.java@Query(nativeQuery = true,
value = "SELECT * " +
"FROM stores s " +
"INNER JOIN products p " +
"ON p.id = s.product_id " +
"INNER JOIN branches b " +
"ON b.id = s.branch_id " +
"WHERE (p.name LIKE CONCAT('%', :productName , '%') OR :productName IS NULL) " +
" AND (b.name LIKE CONCAT('%', :branchName , '%') OR :branchName IS NULL) " +
" AND (:minPrice <= p.price AND p.price <= :maxPrice) " +
" AND s.quantity <= :quantity " +
" ORDER BY p.name")
List<Store> searchStoresByFilter(@Param("productName") String productName, @Param("branchName") String branchName,
@Param("minPrice") double minPrice, @Param("maxPrice") double maxPrice, @Param("quantity") int quantity);
@Query(nativeQuery = true,
value = "SELECT * " +
"FROM stores s " +
"INNER JOIN products p " +
"ON p.id = s.product_id " +
"INNER JOIN branches b " +
"ON b.id = s.branch_id " +
"WHERE (p.name LIKE CONCAT('%', :productName , '%') OR :productName IS NULL) " +
" AND (b.name LIKE CONCAT('%', :branchName , '%') OR :branchName IS NULL) " +
" AND (:minPrice <= p.price AND p.price <= :maxPrice) " +
" AND s.quantity <= :quantity " +
" ORDER BY p.name")
List<Store> searchStoresByFilter(@Param("productName") String productName, @Param("branchName") String branchName,
@Param("minPrice") double minPrice, @Param("maxPrice") double maxPrice, @Param("quantity") int quantity);
通知:
我在我的[[POJO类中被标记为它们从javax.persistence包中发出有关@ Table和@ JoinColumn的警告[@ Table]中的[ @ JoinColumnCannot resolve table 'table_name'
]和Cannot resolve column 'product_id'
Cannot resolve column 'branch_id'
Store.java (Pojo)Product.java (Pojo)
我想知道根本原因
为什么当我从Frontend调用API时为什么不能得到有效的结果?为什么我不能从邮递员调用API?
我创建了一个API,它称为搜索商店功能。 。 MySql脚本当我使用此SQL脚本时,将在MySql中运行。它将显示有效结果SET @productName = null; SET @BranchName = null; SET @ ...
这就是hibernate无法实际映射表的原因,您需要先解决映射问题,以便hibernate知道要使用哪个表。目前,它正在sql中查找“ table_name”,而不是您的商店或产品表。我们不知道您的映射是否正确,因为此处未共享您的实体类代码,但是注释或xml文件可能由于错误本身而具有错误的映射名称,最终使整个请求崩溃。编辑:请检查您的类路径中是否包含这些库:休眠注释lib / hibernate-comons-annotations.jar lib / ejb3-persistence.jar
此外,我建议您使用休眠查询语言而不是本机查询,因为当您要切换基础数据库时,它们更可扩展。
根本原因是,休眠是数据库抽象层(又名DBAL),该库的全部目的是代表您的代码构建查询并执行它们。您正在尝试将其用作本机sql包装器,并且sql查询会将结果提供给休眠状态,但是由于映射错误,它无法解析并在实体中存储数据,尽管在某些情况下它很有用,但休眠状态本身提供了更多强大的功能,例如延迟加载,实体关系仅举几例。
您可以在此处阅读完整的文档以使用这些功能。Getting started guide
以上是关于Spring boot 3问题:为什么不能使用Postman和Frontend调用API调用API,然后得到空数组?的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Sleuth 在我的 Spring Boot 服务中不能与 Log4j2 一起使用
spring boot:实现jpa+thymeleaf 用户管理
spring boot:实现jpa+thymeleaf 用户管理