Tag: spring boot

使用Native Query从Spring Data返回自定义对象

我的问题是基于另一篇文章 。 如何使用本机查询实现相同的function? 本机查询不允许JPQL因此也不允许新实例。 我的POJO。 class Coordinates { private final BigDecimal latitude private final BigDecimal longitude … } 我的数据库表包含城市周长的坐标,因此有三列:city_name,纬度,经度。 每个城市都包含很多(实际上很多)周边坐标,用于在Google地图中构建阴影区域。 我打算在该表上构建一个简单的本机查询,该查询应该返回一个坐标列表。

Tomcat需要太多时间才能启动 – Java SecureRandom

请不要将其标记为重复。 对于这两个问题,这是一个后续问题。 Tomcat7在Ubuntu 14.04 x64上启动太晚[Digitalocean] https://stackoverflow.com/a/2325109/6785908 我理解,替换 securerandom.source=file:/dev/urandom 同 securerandom.source=file:/dev/./urandom 在$JAVA_PATH/jre/lib/security/java.security中将解决这个问题。 我的问题是,在制作中这样做是否可以? 这会对安全性产生什么影响(如会话ID变得可预测)? 如果这不太安全,还有其他方法可以更快地提供足够的熵吗? 更新 我使用openstack进行部署(或者只是说,使用AWS或GCP或任何其他云提供商)。 因此,添加声卡等硬件设备对我来说不是一个选择。

如何为所有控制器配置默认的@RestController URI前缀?

我知道您可以在application.properties设置server.contextPath来更改根上下文。 另外,我可以在Spring Boot的应用程序配置中添加一个额外的上下文,如下例(在Groovy中),为根上下文的URL映射添加“/ api”: @Bean ServletRegistrationBean dispatcherServlet() { ServletRegistrationBean reg = new ServletRegistrationBean(new DispatcherServlet(), “/”) reg.name = “dispatcherServlet” reg.addInitParameter(“contextConfigLocation”, “”) reg.addUrlMappings(“/api/*”) reg.loadOnStartup = 2 reg } } 我正在尝试为Web服务调用提供一个单独的基本URI“/ api”,我可以利用它来保证安全性等。但是使用上述方法将意味着我可以通过以下方式访问我的任何URI,无论是否使用Web服务“/”或“/ api”,并没有提供具体的隔离。 是否有人知道使用配置为所有@RestController设置基本路径的更好方法,而不必使用/ api /正式为每个控制器添加前缀? 如果我被迫手动为每个控制器的URI添加前缀,则可能会错误地省略它并绕过我特定于Web服务的安全措施。 这是Stack Overflow中对同一类型问题的引用,但从未完全回答过: Spring Boot:为RestControllers配置url前缀

Spring Boot SpEL ConditionalOnExpression检查多个属性

如何使用Spring Expression Language检查2个布尔属性是否为真? 例如,检查单个属性是否为true将使用以下语法: @ConditionalOnExpression(“${property.from.properties.file}”) 检查property1 == true && property2 == false的语法是什么? 属性可能具有不同的值。 来自类似问题的答案: 如何在使用@ConditionalOnProperty或@ConditionalOnExpression时将两个字符串连接在一起并执行检查,如下所示: @ConditionalOnExpression(“‘${com.property1}${com.property2}’==’value1value2′”) 对于那些阅读该代码的人来说,这种语法似乎令人困惑,而且这似乎是一个hacky解决方案 我想找到检查两个单独属性而不连接值的正确方法。 另外要明确一点,答案不是你可以从我看到的内容中轻松搜索出来的。 这似乎是一个非常简单的答案,但事实certificate这是相当难以捉摸的。

在简单的示例项目中更改Spring框架日志级别?

在遵循这个 Spring示例时,我希望看到这样的输出: Creating tables Inserting customer record for John Woo Inserting customer record for Jeff Dean … 相反,我在每一行之间散布了一些DEBUG日志消息: Creating tables 12:31:16.474 [main] DEBUG osjdbc.core.JdbcTemplate – Executing SQL statement [drop table customers if exists] 12:31:16.484 [main] DEBUG osjdbc.datasource.DataSourceUtils – Fetching JDBC Connection from DataSource 12:31:16.484 [main] DEBUG osjdSimpleDriverDataSource – Creating new JDBC Driver Connection to [jdbc:h2:mem] […]

Spring Boot 1.2.5.RELEASE – 通过Gmail SMTP发送电子邮件

首先,我需要说发送1.2.0.RELEASE的电子邮件工作正常 application.properties: spring.mail.host = smtp.gmail.com spring.mail.username = *****@gmail.com spring.mail.password = **** spring.mail.properties.mail.smtp.auth = true spring.mail.properties.mail.smtp.socketFactory.port = 465 spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory spring.mail.properties.mail.smtp.socketFactory.fallback = false pox.xml org.springframework.boot spring-boot-starter-parent 1.2.0.RELEASE ……. org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-mail 将父版本更改为1.2.5.RELEASE电子邮件发送无效 文档说: 如果spring.mail.host和相关库(由spring-boot-starter-mail定义)可用,则创建默认JavaMailSender(如果不存在)。 所以我补充说 javax.mail mail 1.4.7 它没有帮助,然后我已经取而代之 com.sun.mail javax.mail 1.5.4 我也试过了 spring.mail.host = smtp.gmail.com spring.mail.username = *****@gmail.com spring.mail.password = **** spring.mail.port = […]

Spring Boot – 使用ResourceLoader读取文本文件

我正在尝试使用Spring资源加载器读取文本文件,如下所示: Resource resource = resourceLoader.getResource(“classpath:\\static\\Sample.txt”); 该文件位于我的Spring启动项目中: 它在eclipse中运行应用程序时工作正常,但是当我打包应用程序然后使用java -jar运行它时,我得到文件未找到exception: java.io.FileNotFoundException: class path resource [static/Sample.txt] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/C:/workspace-test/XXX/target/XXX-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/static/Sample.txt 我解压缩了Sample所在的Jar文件:XXX-0.0.1-SNAPSHOT \ BOOT-INF \ classes \ static \ Sample.txt 有人能帮助我吗 ? 提前致谢!

如何将application.properties中的值赋给静态变量?

我正在使用Spring MVC。 我有一个用@Service注释的UserService类,它有很多静态变量。 我想用application.properties文件中的值来实例化它们。 例如,在application.properties中我有: SVN_URL = http://some.url/repositories 然后在类中有: @Value(“${SVN_URL}”) private static String SVN_URL 我得到Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError的Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError 我也试过@Autowired private static Environment env; 然后: private static String SVN_URL=env.getProperty(“SVN_URL”); 它给出了同样的错误。

使用spring boot实现2路SSL

我正在创建一些宁静的Web服务,并使用Spring-Boot创建一个嵌入式tomcat容器。 其中一个要求是实现双向SSL。 我一直在查看HttpSecurity对象,并且可以使用它来仅通过SSL通道运行web服务: – @Override protected void configure(HttpSecurity http) throws Exception { System.out.println(“CONFIGURED”); http // … .requiresChannel() .anyRequest().requiresSecure(); } 我似乎无法找到的方法是只允许提供有效客户端证书的应用程序访问Web服务。 我只有SSL的基本知识,所以即使是正确方向的一般指针也会受到赞赏。 正在部署的服务器将具有混合的应用程序 – 这是唯一需要使用双向SSL锁定的应用程序。 我真正想要的是一种锁定单个应用程序只接受客户端证书的方法。

使用Spring Boot和多个模块的Maven配置 – 在Intellij中运行应用程序

我目前正在使用Spring Boot开发REST API。 我是Maven的新手,刚开始使用IDEA编码(还不熟悉这个IDE),我有一个问题…… 这是我的项目结构: 亲 的pom.xml 主要模块 调节器 域 App.java(Spring Boot主类) 的pom.xml 子模块(需要主模块作为依赖) 控制器 域 的pom.xml 因此,当我在Intellij中运行项目时,它会启动,我可以访问主模块控制器中定义的所有URL。 但不是子模块控制器中的那些……看起来只有主模块被加载了。 这是我的父pom.xml: org.springframework.boot spring-boot-starter-parent 1.2.1.RELEASE 4.0.0 Test :: Test :: Parent POM test.test.test project-parent 0.0.1-SNAPSHOT pom 1.8 1.8 1.2.1.RELEASE 2.5 java jacoco reuseReports ${project.basedir}/../target/jacoco.exec 2.18.1 0.7.3.201502191951 UTF-8 UTF-8 submodule main org.apache.maven.plugins maven-surefire-plugin ${maven-surefire-plugin.version} true **/*Test.java **/*IT.java **/*Story.java […]