我收到以下异常:
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value did not match expected type. [java.util.Date (n/a)];
nested exception is java.lang.IllegalArgumentException: Parameter value did not match expected type [java.util.Date (n/a)]
Run Code Online (Sandbox Code Playgroud)
这是我的存储库中的查询方法:
@Query("SELECT COUNT(entity) FROM Person entity WHERE trunc(entity.date) BETWEEN :startTime AND :endTime")
int getPersonBetweenDates(@Param("startTime") DateTime var1, @Param("endTime") DateTime var2);
Run Code Online (Sandbox Code Playgroud)
我在组件中的实现:
int totalPersons = personRepo.getPersonBetweenDates(new DateTime().withTimeAtStartOfDay(), DateTime.now());
Run Code Online (Sandbox Code Playgroud)
为什么我收到这个错误?实现中的两个DateTime参数似乎与我的方法中的参数相匹配?
我正在读关于DatabaseConfig
在光滑的文档:
除了配置语法之外
Database
,还有另一个层,其形式DatabaseConfig
允许您配置Slick驱动程序和Database
一起匹配.这样,只需更改配置文件,就可以轻松地在不同类型的数据库系统上进行抽象.
我没有得到这一部分,如何DatabaseConfig
使底层数据库系统比Database
方法更抽象?假设,我DatabaseConfig
在以下测试中使用:
import org.scalatest.{Matchers, FlatSpec}
import slick.backend.DatabaseConfig
import slick.driver.JdbcProfile
import slick.driver.PostgresDriver.api._
import scala.concurrent.ExecutionContext.Implicits.global
class DatabaseConfigTest extends FlatSpec with Matchers {
def withDb(test: DatabaseConfig[JdbcProfile] => Any) = {
val dbConfig = DatabaseConfig.forConfig[JdbcProfile]("abstract")
try test(dbConfig)
finally dbConfig.db.close()
}
"DatabaseConfig" should "work" in withDb { dbConfig =>
import Supplier._
val cities = suppliers.map(_.city)
dbConfig.db.run(cities.result).map(_.foreach(println))
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,如果我将基础数据库系统从更改PostgreSQL
为MySQL
,除了配置更改之外,我还需要更改import
将postgre API导入mysql的语句.另一方面,如果我使用 …
我试图将不安全的控制器端点添加/foo/bar
到我的应用程序中,但是每当我尝试调用它时,我都会得到401 Unauthorized
。
这是我的WebSecurityConfigurerAdapter
:
http
.authorizeRequests()
.antMatchers("/foo/**").permitAll()
.and()
.formLogin()
.loginPage("/login").permitAll()
.and()
.requestMatchers()
.antMatchers("/login", "/oauth/authorize", "/oauth/confirm_access")
.and()
.authorizeRequests()
.anyRequest().authenticated();
Run Code Online (Sandbox Code Playgroud)
somone是否会指出我想念的是什么?
我想知道是否可以RestController
仅基于请求正文将相同的 URL 映射到类中的不同方法。例如:
@RequestMapping(value="/delete", method=RequestMethod.POST )
public void delete(@RequestBody String id) {
//do something
}
@RequestMapping(value="/delete", method=RequestMethod.POST )
public void delete(@RequestBody Book book) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
请求正文将始终是 JSON 有效负载。如果是{"id":"foo"}
我想要调用第一个方法。如果请求正文是:
{
"title":"Spring Guide",
"author":"John Doe"
}
Run Code Online (Sandbox Code Playgroud)
我想调用第二种方法。这可能吗?
Slick 3有"import api"来使用特定的数据库驱动程序.例如
import slick.driver.H2Driver.api._
...DAO implementation...
Run Code Online (Sandbox Code Playgroud)
要么
import slick.driver.PostgresDriver.api._
...DAO implementation...
Run Code Online (Sandbox Code Playgroud)
如何在生产中使用postgresql,在单元测试中使用h2?
我有一个使用Hibernate和Spring Data JPA的Spring应用程序CrudRepository
.如果查询的数据存在于数据库中,则一切似乎都能正常工作.但是,如果有一个返回没有结果的查询,那么CrudRepository
返回null
并得到一个NullPointerException
.因此,例如,http://localhost:8080/api/id=3
如果数据库中有一个id为3的行,它可以正常工作.如果没有id为3的行,则失败并显示:
出现意外错误(type = Internal Server Error,status = 500)
在客户端和NullPointerException
服务器端.
处理"无结果"查询的简单情况的正确方法是什么?
我正在尝试使用 spring 为我的应用程序构建缓存服务。缓存需要从数据库填充。
我的应用程序在三个节点上运行,并希望所有三个节点都与缓存同步。如果一个节点在缓存中获得更新的值,它应该通知其他节点。
我查看了Spring Cache 抽象,它没有谈论集群环境中的缓存。
有没有办法将缓存通知传播到其他节点?
在我的项目中,我开始使用Spring Boot Actuator.我使用/shutdown
端点来优雅地停止嵌入式Tomcat(这很好用),但我还需要在关机期间做一些自定义逻辑.有什么办法,该怎么办?
我创建了一个认证服务器和资源服务器,两者都工作正常,唯一的问题是刷新令牌,我想它调用后更改POST /oauth/token
用grant_type=refresh_token
,但是,春回同样刷新令牌.
我想知道在调用oauth端点刷新访问令牌时是否有办法获得新的刷新令牌?
java spring spring-security oauth-2.0 spring-security-oauth2
几天前,我被问到这个程序的输出:
public static void main(String[] args) {
// \u0022 is the Unicode escape for double quote (")
System.out.println("a\u0022.length() + \u0022b".length());
}
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是这个程序应该打印a\u0022.length() + \u0022b
长度,16
但令人惊讶的是,它打印出来2
.我知道\u0022
是unicode,"
但我认为这"
将被转义,只代表一个"
文字,没有特殊意义.实际上,Java以某种方式解析了这个字符串如下:
System.out.println("a".length() + "b".length());
Run Code Online (Sandbox Code Playgroud)
我无法绕过这种奇怪的行为,为什么Unicode转义不像正常的转义序列那样?
更新显然,这是Java Puzzlers的脑筋急转弯:由Joshua Bloch和Neal Gafter撰写的陷阱,陷阱和角落案例.更具体地说,这个问题与Puzzle 14:Escape Rout有关.
java ×8
spring ×7
spring-mvc ×3
scala ×2
slick ×2
caching ×1
hibernate ×1
jodatime ×1
oauth-2.0 ×1
oracle11g ×1
spring-boot ×1
spring-cache ×1
spring-data ×1