我是新安装的
IntelliJ IDEA 2021.2 (Ultimate Edition)
Build #IU-212.4746.92, built on July 27, 2021
Licensed to XXXXXX
Subscription is active until August 15, 2021.
Runtime version: 11.0.11+9-b1504.13 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.4.0-80-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 3
Kotlin: 212-1.5.10-release-IJ4746.92
Current Desktop: X-Cinnamon
Run Code Online (Sandbox Code Playgroud)
我克隆了我在其他工作站上使用的项目,没有问题,但无法使用 main 方法启动任何类,IDEA 说:
Abnormal build process termination:
/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin/java -Xmx700m -Djava.awt.headless=true -Djava.endorsed.dirs=\"\" -Dcompile.parallel=false -Drebuild.on.dependency.change=true -Djdt.compiler.useSingleThread=true -Daether.connector.resumeDownloads=false -Dio.netty.initialSeedUniquifier=-5972351880001011455 -Dfile.encoding=UTF-8 -Duser.language=en -Duser.country=US -Didea.paths.selector=IntelliJIdea2021.2 -Didea.home.path=/home/pm/idea-IU-212.4746.92 -Didea.config.path=/home/pm/.config/JetBrains/IntelliJIdea2021.2 -Didea.plugins.path=/home/pm/.local/share/JetBrains/IntelliJIdea2021.2 -Djps.log.dir=/home/pm/.cache/JetBrains/IntelliJIdea2021.2/log/build-log -Djps.fallback.jdk.home=/home/pm/idea-IU-212.4746.92/jbr -Djps.fallback.jdk.version=11.0.11 …Run Code Online (Sandbox Code Playgroud) 我@Service在Spring Boot应用程序中创建了一个类,其中一个方法应该异步运行.因为我读取方法应该@Async注释,我也必须运行一个TaskExecutorbean.但是在Spring手册http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html我没有找到任何信息或示例如何TaskExecutor使用注释运行,没有XML配置.是否可以TaskExecutor在没有XML的情况下在Spring Boot中创建bean,仅注释?这是我的服务类:
@Service
public class CatalogPageServiceImpl implements CatalogPageService {
@Override
public void processPagesList(List<CatalogPage> catalogPageList) {
for (CatalogPage catalogPage:catalogPageList){
processPage(catalogPage);
}
}
@Override
@Async("locationPageExecutor")
public void processPage(CatalogPage catalogPage) {
System.out.println("print from Async method "+catalogPage.getUrl());
}
}
Run Code Online (Sandbox Code Playgroud) 我在远程GIT存储库中创建了一个新分支.存储在BitBucket上的存储库.
在我的本地存储库中,我可以看到这个带有SourceTree的新分支.它出现在提交列表中,在同一个提交中我创建了分支.但是我看不到分支列表下的这个新分支,也无法查看它.
我想检查并将我本地工作站代码的更新推送回远程存储库,但在这个新分支中.我在同一个分支上开始开发,克隆新分支的来源是什么.
我试图拉这个新的分支.它应该是空的,但它仍然不在分支列表下,我无法检查它.
如何投入这个新分支?
我有两个 Spring Boot 项目,并且想将其中一个用作另一个中的 MAVEN 依赖项。
在数据库项目中,我必须覆盖 Hibernate 版本,并像https://spring.io/blog/2016/04/13/overriding-dependency-versions-with-spring-boot选项 2 中描述的那样。
<properties>
...
<hibernate.version>5.2.10.Final</hibernate.version>
</properties>
Run Code Online (Sandbox Code Playgroud)
这工作正常,我在 MAVEN 依赖项中看到数据库项目的正确版本:/home/pm/.m2/repository/org/hibernate/hibernate-core/5.2.10.Final/hibernate-core-5.2.10.Final.jar
比我来的 Scraper 项目。它在 Maven 依赖项中不包含任何 Hibernate,因为我没有明确导入它们。现在我添加我的数据库项目依赖项
<dependency>
<groupId>web.scraper.database</groupId>
<artifactId>DataBase</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这也导入了 hibernate,但版本错误(Spring 父 pom 中提到的版本,而不是我的数据库项目 Hibernate 版本)/home/pm/.m2/repository/org/hibernate/hibernate-core/5.0.12.Final / hibernate-core-5.0.12.Final.jar
我想获得数据库项目依赖中提到的休眠版本。何做这个?
我不想在 Scraper 项目中覆盖 Hibernate 版本。
刮板/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>web.scraper.engine</groupId>
<artifactId>Scraper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Scraper</name>
<description>Web scraper application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository --> …Run Code Online (Sandbox Code Playgroud) 我的应用程序使用 Spring Boot、Hibernate、JUnit 构建。我使用 H2 数据库进行内存测试,并创建了一个 bean 以便能够使用 SQL 工具连接到 H2 数据库。
@Configuration
public class H2DataBaseTestConfig {
@Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException {
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
}
}
Run Code Online (Sandbox Code Playgroud)
如果我运行一个测试,它工作正常,但如果我运行所有测试,我会在第二个和所有下一个测试类上收到错误
Exception opening port "9092" (port may be in use)
Run Code Online (Sandbox Code Playgroud)
这看起来像 Spring 尝试创建应用程序上下文并在每次运行下一个测试类时启动 bean,尽管这与它自己打开的端口有某种冲突。我怎样才能运行所有测试并使这个 bean 正常工作?
更新
由于很多人提到测试类注释,我将在这里展示它们:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(IndeedScraper.class)
@TestPropertySource(locations="classpath:test.properties")
Run Code Online (Sandbox Code Playgroud)
我有几个 @Configuration 类:两个数据库连接/事务/实体管理类,并在此处描述 h2Server bean。h2Server bean builder类存储在src/test/java中,而其他@Configuration类存储在src/main/java中
结束更新
这里完整的堆栈跟踪供参考:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) …Run Code Online (Sandbox Code Playgroud) 我需要在日志中包含映射的诊断上下文数据。应用程序构建:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
Run Code Online (Sandbox Code Playgroud)
为了在日志中获取 MDC,我将其放入application.properties文件中:
logging.pattern.level=%X{mdcData}%5p
Run Code Online (Sandbox Code Playgroud)
并创建一个类
@Component
public class RequestFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
try {
// Setup MDC data:
String mdcData = String.format("[userId:%s | requestId:%s] ", "hello", "hello");
MDC.put("mdcData", mdcData); //Variable 'mdcData' is referenced in Spring Boot's logging.pattern.level property
chain.doFilter(request, response);
} finally {
// Tear down MDC data:
// ( Important! Cleans …Run Code Online (Sandbox Code Playgroud) 如何创建适当的测试环境,以便能够在同一应用程序中使用数据库层测试和带有模拟的 REST 端点测试?
我有一个带有两个数据源的 Spring Boot 应用程序。用于管理 Atomikos 使用的交易。这个配置工作正常。
现在我需要创建测试。我构建了一个测试配置,每个测试都工作正常,但当我运行所有测试时它都会失败。在我看来(参见堆栈跟踪),问题是如果实例化很少的 Atomikos beans,Atomikos 就无法工作。
我尝试了两种解决方案来使 Atomikos beans 仅实例化一次:
创建一个用于所有测试的测试配置(因为 Spring 缓存测试上下文)。但这不起作用。我认为这是因为 @Controller 中的 Mock beans 破坏了重用 Spring 测试上下文的能力。我在测试中使用的持久性映射器组件在一个测试中被模拟,同时在其他测试中使用真实实例。所以我看到每个测试类都在它自己的测试上下文中运行。
在数据库 @Configuration 类上使用 @Lazy 注释。我认为这将确保 bean 仅在第一次调用时才会被实例化,并将在以后的调用中重用。但这也行不通。
这是我用来说明问题的示例项目链接。该存储库包括 MySQL 数据库转储: https: //github.com/pavelmorozov/AtomikosConfig
在这篇文章中,我将仅展示两个数据库模型、映射器和配置类中的一个,因为它们对于第二个数据库几乎相同。
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at …Run Code Online (Sandbox Code Playgroud) I cannot access some of https resources. Please help to make https calls reliable. Examples I put here tested from Firefox browser, to ensure they are works properly.
$ java -version
openjdk version "1.8.0_121"
OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-1~bpo8+1-b13)
OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
Run Code Online (Sandbox Code Playgroud)
I also checked website where https connection works and others, that are fail( errors samples here) are seems use same connection encryption: TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 128 bit keys, TLS 1.2
I need to collect data …
我有Spring启动应用程序和LocalDateTime属性序列化为JSON数组,而不是字符串。
正如我在网络上搜索时所发现的,我所需要做的就是在application.properties中进行设置
spring.jackson.serialization.write-dates-as-timestamps=false
Run Code Online (Sandbox Code Playgroud)
我还尝试将jackson-datatype-jsr310放入依赖项,但也没有运气
我也尝试添加注释:
@DateTimeFormat(iso = ISO.DATE_TIME)
Run Code Online (Sandbox Code Playgroud)
这也没有帮助。
我看到很多人遇到类似的情况,但是他们的解决方案似乎与Spring Boot 1.x有关,而我使用的是2.04
我也使用Lombok,但不确定是否会影响序列化格式。
如何跟踪日期序列化格式并将其固定为ISO日期字符串?
这里的响应示例(开始是LocalDateTime,我想将其作为ISO字符串):
{
"id": 3,
"enabled": true,
"outletId": 5,
"reason": "hello",
"start": [
2019,
9,
10,
10,
42,
11
],
"status": "AVAILABLE"
}
Run Code Online (Sandbox Code Playgroud)
此处的REST控制器方法响应对象:
@Data
@Entity
@Table(indexes = { @Index(columnList = ("outletId"),name="outlet_id_index"),
@Index(columnList = ("start"),name="start_index"),
@Index(columnList = ("outletId, start"),name="outlet_id_start_index")})
public class OutletChron extends BaseEntity {
private Long outletId;
private String reason;
@DateTimeFormat(iso = ISO.DATE_TIME)
private LocalDateTime start;
@Enumerated(EnumType.STRING)
@Column(length = 30)
private OutletStatus …Run Code Online (Sandbox Code Playgroud) 我在 Spring Boot 应用程序中使用 Spring Security 配置了 JWT 安全性。我有一个问题
Access-Control-Allow-Origin: *
Run Code Online (Sandbox Code Playgroud)
标头,也称为 CORS。我配置了应用程序,因此每个服务器响应中都存在标头,但一旦 JWT 令牌无效,服务器就会响应 403 错误代码,而没有 Access-Control-Allow-Origin: * 标头。这会导致浏览器将错误消息写入控制台:
无法加载 http://... 请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问来源“http://...”。响应的 HTTP 状态代码为 403。
这似乎是错误的,我希望获得 Access-Control-Allow-Origin: * 标头作为响应,即使 JWT 令牌无效且服务器响应带有 403 错误代码。
现在我尝试过的和我的代码。
依赖项:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
据我了解,这个问题可能是由过滤器链中的过滤器顺序引起的,我尝试将 JWT JwtAuthenticationFilter 放在 CorsFilter 或 CsrfFilter 之后,创建 CorsConfigurationSource bean。这在https://docs.spring.io/spring-security/site/docs/current/reference/html5/#cors中进行了描述,并在如何在 Spring Boot + Spring Security 应用程序中配置 CORS?和https://github.com/spring-projects/spring-boot/issues/5834,但似乎没有任何帮助
@Configuration
@EnableWebSecurity
public class …Run Code Online (Sandbox Code Playgroud)