热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:要在API请求中发布的SpringJPA

篇首语:本文由编程笔记#小编为大家整理,主要介绍了要在API请求中发布的SpringJPA相关的知识,希望对你有一定的参考价值。

篇首语:本文由编程笔记#小编为大家整理,主要介绍了要在API请求中发布的Spring JPA相关的知识,希望对你有一定的参考价值。



[我在这里尝试通过findby函数从mysql数据库中逐行获取数据,并且工作正常,但是下一步我试图访问一个API,以通过http POST将所需的数据从我的表填充到API并将processDate更改为当前日期,但是在尝试实现commandlineruner]时出现错误

错误:

java.lang.IllegalStateException:无法执行CommandLineRunner,引起原因:java.lang.IllegalMonitorStateException:null

API格式如下:

{
"ChannelID":"113", # static string no change on it
"MSISDN":"XXX", # i have to fill it wil my msisdn
"ServiceID":"XXX" i have to fill it with my param1
}

AccessingDataJpaApplication类:

@SpringBootApplication
public class AccessingDataJpaApplication implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(AccessingDataJpaApplication.class);
@Autowired
private Bulk_repositoryRepository bulk_repositoryRepository;
public static void main(String[] args) {
SpringApplication.run(AccessingDataJpaApplication.class);
}
Date currentDate = new Date();
@Override
public void run(String... args) throws Exception {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
headers.setBasicAuth("######", "#####");
while(true) {
for (Bulk_repository churnss : bulk_repositoryRepository.findAllByStatusAndCampTypeAndCampStartDateLessThanAndCampEndDateGreaterThanEqual(0,54,currentDate,currentDate)) {
logger.info(churnss.toString());
AddOfferRequest AddOffer = new AddOfferRequest("113", churnss.getMsisdn(),churnss.getParam1());
logger.info(AddOffer.toString());
HttpEntity entity = new HttpEntity(AddOffer,headers);
ResponseEntity respOnseEntity= restTemplate.exchange(
"API LINK.......", HttpMethod.POST, entity, String.class);
if(responseEntity.getStatusCode() == HttpStatus.OK){
String respOnse= responseEntity.getBody();
logger.info(response);
churnss.setStatus(1);
churnss.setProcessDate(new Date());
logger.info(churnss.toString());
bulk_repositoryRepository.save(churnss);
}
}
wait(1000);
}
}
}

AddOfferRequest类:

public class AddOfferRequest {
private String ChannelID="113";
private String MSISDN;
private String ServiceID;
public AddOfferRequest() {
}
public AddOfferRequest(String channelID,String mSISDN,String serviceID ) {
this.MSISDN = mSISDN;
this.ServiceID = serviceID;
}
public void setMSISDN(String mSISDN) {
this.MSISDN = mSISDN;
}
public void setServiceID(String serviceID) {
this.ServiceID = serviceID;
}
public String getChannelID() {
return ChannelID;
}
public String getMSISDN() {
return MSISDN;
}
public String getServiceID() {
return ServiceID;
}
}

Bulk_repository类:

@Entity
@Table(name = "BULK_REPOSITORY")
public class Bulk_repository {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id")
private long id;
@Column(name = "msisdn")
private String msisdn;
@Column(name = "camp_start_date")
private Date campStartDate;
@Column(name = "camp_end_date")
private Date campEndDate;
@Column(name = "camp_type")
private int campType;
@Column(name = "camp_cd")
private String camp_cd;
@Column(name = "status")
private int status;
@Column(name = "process_date")
private Date processDate;
@Column(name = "entry_date")
private Date entryDate;
@Column(name = "entry_user")
private String entry_user;
@Column(name = "param1")
private String param1;
@Column(name = "param2")
private String param2;
@Column(name = "param3")
private String param3;
@Column(name = "param4")
private String param4;
@Column(name = "param5")
private String param5;
@Column(name = "error_desc")
private String error_desc;
@Column(name = "fulfilment_status")
private int fulfilment_status;
## then getter and setters and to tostring

Bulk_repositoryRepository类:

public interface Bulk_repositoryRepository extends CrudRepository {
ListfindAllByStatusAndCampTypeAndCampStartDateLessThanAndCampEndDateGreaterThanEqual(int status, int campType,Date currentDate, Date currentDate1);
Bulk_repository findById(long id);
}

stacktrace:

2019-10-26 17:25:47.563 INFO 10275 --- [ main] c.e.a.AccessingDataJpaApplication : Starting AccessingDataJpaApplication on ubuntu with PID 10275 (/home/devops/Documents/workspace-spring-tool-suite-4-4.4.0.RELEASE/gs-accessing-data-jpa-complete/target/classes started by devops in /home/devops/Documents/workspace-spring-tool-suite-4-4.4.0.RELEASE/gs-accessing-data-jpa-complete)
2019-10-26 17:25:47.572 INFO 10275 --- [ main] c.e.a.AccessingDataJpaApplication : No active profile set, falling back to default profiles: default
2019-10-26 17:25:50.970 INFO 10275 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-10-26 17:25:51.141 INFO 10275 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 149ms. Found 1 repository interfaces.
2019-10-26 17:25:51.948 INFO 10275 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$58bd9157] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-10-26 17:25:52.578 INFO 10275 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-10-26 17:25:52.693 INFO 10275 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-10-26 17:25:52.711 INFO 10275 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.22]
2019-10-26 17:25:53.002 INFO 10275 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-10-26 17:25:53.003 INFO 10275 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5219 ms
2019-10-26 17:25:53.389 INFO 10275 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-10-26 17:25:53.790 INFO 10275 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-10-26 17:25:53.891 INFO 10275 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-10-26 17:25:54.339 INFO 10275 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.3.10.Final}
2019-10-26 17:25:54.342 INFO 10275 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-10-26 17:25:54.755 INFO 10275 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-10-26 17:25:55.225 INFO 10275 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-10-26 17:25:56.861 INFO 10275 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-10-26 17:25:54.214 INFO 10275 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-26 17:25:54.299 WARN 10275 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-10-26 17:25:54.798 INFO 10275 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-26 17:25:54.804 INFO 10275 --- [ main] c.e.a.AccessingDataJpaApplication : Started AccessingDataJpaApplication in 9.283 seconds (JVM running for 16.131)
2019-10-26 17:25:54.908 INFO 10275 --- [ main] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
2019-10-26 17:25:55.220 INFO 10275 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-10-26 17:25:55.236 ERROR 10275 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:783) ~[spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:764) ~[spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:319) ~[spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214) ~[spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203) ~[spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
at com.example.accessingdatajpa.AccessingDataJpaApplication.main(AccessingDataJpaApplication.java:38) ~[classes/:na]
Caused by: java.lang.IllegalMonitorStateException: null
at java.base/java.lang.Object.wait(Native Method) ~[na:na]
at com.example.accessingdatajpa.AccessingDataJpaApplication.run(AccessingDataJpaApplication.java:71) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:780) ~[spring-boot-2.1.7.RELEASE.jar:2.1.7.RELEASE]
... 5 common frames omitted
2019-10-26 17:25:55.269 INFO 10275 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2019-10-26 17:25:55.270 INFO 10275 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2019-10-26 17:25:55.278 INFO 10275 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2019-10-26 17:25:55.298 INFO 10275 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.

这里我正尝试使用findby函数从mysql数据库中逐行获取数据,并且工作正常,但是下一步我试图访问一个API,以将所需的数据从我的表一直填充到API中……] >

答案

您需要处于同步块中,Object.wait()才能工作。它在多线程环境中用于同步线程。但是我假设您只是希望您的代码在记录最新结果之间稍等一下。

改为使用Thread.sleep(1000)


推荐阅读
  • 探讨如何真正掌握Java EE,包括所需技能、工具和实践经验。资深软件教学总监李刚分享了对毕业生简历中常见问题的看法,并提供了详尽的标准。 ... [详细]
  • 解读MySQL查询执行计划的详细指南
    本文旨在帮助开发者和数据库管理员深入了解如何解读MySQL查询执行计划。通过详细的解析,您将掌握优化查询性能的关键技巧,了解各种访问类型和额外信息的含义。 ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • 本文详细介绍了 MySQL 中 LAST_INSERT_ID() 函数的使用方法及其工作原理,包括如何获取最后一个插入记录的自增 ID、多行插入时的行为以及在不同客户端环境下的表现。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • 使用GDI的一些AIP函数我们可以轻易的绘制出简 ... [详细]
  • 本文探讨了如何在 PHP 的 Eloquent ORM 中实现数据表之间的关联查询,并通过具体示例详细解释了如何将关联数据嵌入到查询结果中。这不仅提高了数据查询的效率,还简化了代码逻辑。 ... [详细]
  • 深入解析 Apache Shiro 安全框架架构
    本文详细介绍了 Apache Shiro,一个强大且灵活的开源安全框架。Shiro 专注于简化身份验证、授权、会话管理和加密等复杂的安全操作,使开发者能够更轻松地保护应用程序。其核心目标是提供易于使用和理解的API,同时确保高度的安全性和灵活性。 ... [详细]
  • 微软Exchange服务器遭遇2022年版“千年虫”漏洞
    微软Exchange服务器在新年伊始遭遇了一个类似于‘千年虫’的日期处理漏洞,导致邮件传输受阻。该问题主要影响配置了FIP-FS恶意软件引擎的Exchange 2016和2019版本。 ... [详细]
  • 作者:守望者1028链接:https:www.nowcoder.comdiscuss55353来源:牛客网面试高频题:校招过程中参考过牛客诸位大佬的面经,但是具体哪一块是参考谁的我 ... [详细]
  • 深入理解Redis的数据结构与对象系统
    本文详细探讨了Redis中的数据结构和对象系统的实现,包括字符串、列表、集合、哈希表和有序集合等五种核心对象类型,以及它们所使用的底层数据结构。通过分析源码和相关文献,帮助读者更好地理解Redis的设计原理。 ... [详细]
  • 机器学习中的相似度度量与模型优化
    本文探讨了机器学习中常见的相似度度量方法,包括余弦相似度、欧氏距离和马氏距离,并详细介绍了如何通过选择合适的模型复杂度和正则化来提高模型的泛化能力。此外,文章还涵盖了模型评估的各种方法和指标,以及不同分类器的工作原理和应用场景。 ... [详细]
  • Hadoop入门与核心组件详解
    本文详细介绍了Hadoop的基础知识及其核心组件,包括HDFS、MapReduce和YARN。通过本文,读者可以全面了解Hadoop的生态系统及应用场景。 ... [详细]
  • 网易严选Java开发面试:MySQL索引深度解析
    本文详细记录了网易严选Java开发岗位的面试经验,特别针对MySQL索引相关的技术问题进行了深入探讨。通过本文,读者可以了解面试官常问的索引问题及其背后的原理。 ... [详细]
  • Struts与Spring框架的集成指南
    本文详细介绍了如何将Struts和Spring两个流行的Java Web开发框架进行整合,涵盖从环境配置到代码实现的具体步骤。 ... [详细]
author-avatar
流浪1种无奈
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有