小编Nit*_*706的帖子

在 spring-boot 中从 jar 中读取文件

我正在使用 Spring boot 2.0.4,尝试读取放置在 resources/ 目录中的 json 文件,但收到“FileNotFoundException”。

我已经从 Stackoverflow 和其他链接引用了很多页面,但到目前为止还没有成功。

以下是堆栈跟踪片段,我不确定为什么会有“!” 在类路径中:

/opt/springboot/myProject.jar!/BOOT-INF/classes!/schema/jsonValidationSchema.json at o.s.u.ResourceUtils.getFile(ResourceUtils.java:217) at o.s.u.ResourceUtils.getFile(ResourceUtils.java:180)
Run Code Online (Sandbox Code Playgroud)

这是代码片段:

        InputStream in = new ClassPathResource("jsonValidationSchema.json").getInputStream();
        JsonNode schemaJsonNode = JsonLoader.fromReader(new InputStreamReader(in, "UTF-8"));
        JsonSchema schemaNode = ValidationUtils.getSchemaNode(schemaJsonNode);
Run Code Online (Sandbox Code Playgroud)

参考:

jar 内的文件对于 spring 不可见

从 Spring Boot 中的资源文件夹中读取文件

https://smarterco.de/java-load-file-from-classpath-in-spring-boot/

java.io.FileNotFoundException:无法打开类路径资源,因为它不存在

java.io.FileNotFoundException:类路径资源

spring java-io spring-boot

7
推荐指数
0
解决办法
1万
查看次数

ClassNotFoundException:org.slf4j.impl.StaticLoggerBinder

我正在将 spring 应用程序转换为 spring-boot,使用 boot-starter-parent 版本:2.0.4.RELEASE。当我使用 mvn install 构建时,它运行良好,但是当我尝试使用命令运行应用程序时:mvn spring-boot:run -Dspring.profiles.active=dev,我收到此异常: ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder

以下是我的 pom 中的依赖项:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
</parent>

<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
    <version>2.0.1.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.24</version>
  </dependency>
  <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.3.0-alpha4</version>
  </dependency>
  <dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.3.0-alpha4</version>
  </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

我尝试遵循此问题的建议,并使用新旧版本的 logback(核心和经典)依赖项,并添加“slf4j-log4j12”和“slf4j-simple”,但仍然出现异常。堆栈跟踪是:

java.lang.NoClassDefFoundError:org/slf4j/impl/StaticLoggerBinder 在 org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext (LogbackLoggingSystem.java:285) 在 org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize (LogbackLoggingSystem) .java:102) 在 org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent (LoggingApplicationListener.java:191) 在 org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent (LoggingApplicationListener.java:170) 在 org. springframework.context.event.SimpleApplicationEventMulticaster.invokeListener (SimpleApplicationEventMulticaster.java:167) 在 org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent (SimpleApplicationEventMulticaster.java:139) 在 org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent (SimpleApplicationEventMulticaster. java:122)在org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:68)在org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)在org.springframework.boot.SpringApplication .run (SpringApplication.java:316) 在 org.springframework.boot.SpringApplication.run (SpringApplication.java:1258) 在 …

logging slf4j spring-boot logback-classic spring-logback

5
推荐指数
1
解决办法
5600
查看次数

如何从 Mono SpringWebFlux 返回对象

我正在使用Spring Webflux,我需要在成功保存后返回用户的ID。

存储库正在返回 Mono

Mono<User> savedUserMono = repository.save(user);
Run Code Online (Sandbox Code Playgroud)

但是从控制器中,我需要返回 save() 调用返回的对象中的用户 ID。

我尝试过使用 doOn* 和 subscribe(),但是当我使用 subscribe 返回时,出现错误“意外的返回值”

reactive-programming project-reactor spring-webflux

5
推荐指数
2
解决办法
2万
查看次数

用于路径列表的 antMatchers

我有一个路径列表,我想提供给antMatchers(). 但是由于这个列表是动态的,我不能提供逗号分隔的路径。我目前正在遍历列表并将 eachPath 添加到antMatcher(). 有没有更好的方法可以将 antMatcher 应用于路径列表?

当前代码片段:

   http.authorizeRequests()
    .antMatchers("/signup","/about")
    .hasRole("ADMIN")
    .anyRequest().authenticated();
Run Code Online (Sandbox Code Playgroud)

我有一个路径列表。例如:

List<String> pathList = Arrays.asList(new String[]{"/signup","/about"});
Run Code Online (Sandbox Code Playgroud)

我想应用 antMatchers 类似的东西:

http.authorizeRequests()
        .antMatchers(pathList)
        .hasRole("ADMIN")
        .anyRequest().authenticated();
Run Code Online (Sandbox Code Playgroud)

参考:https : //spring.io/blog/2013/07/11/spring-security-java-config-preview-readability/ spring security http antMatcher 多路径

spring spring-security

3
推荐指数
1
解决办法
3203
查看次数