我正在尝试使用Jackson将ISO8601格式的日期反序列化为Java8 Instant.我使用ObjectMapper注册了JavaTimeModule,关闭了WRITE_DATES_AS_TIMESTAMPS.
但是,如果尝试反序列化2016-03-28T19:00:00.000 + 01:00它将无法工作,因为似乎JavaTimeModule将仅反序列化使用UTC时区偏移量格式化的日期时间(例如2016-03-28T18:00 :00.000Z).然后我尝试使用@JsonFormat注释,如下所示:
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone = "UTC")
Run Code Online (Sandbox Code Playgroud)
像这样:
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone = JsonFormat.DEFAULT_TIMEZONE)
Run Code Online (Sandbox Code Playgroud)
但是,这些都不起作用,我得到一个例外:
com.fasterxml.jackson.databind.JsonMappingException: Unsupported field: YearOfEra (through reference chain: org.example.Article["date"])
Run Code Online (Sandbox Code Playgroud)
这意味着时区参数被忽略,日期时间格式化程序不知道如何在没有时区的情况下格式化Instant.
有没有办法在没有编写自定义反序列化器的情况下,使用Jackson和JavaTimeModule将不在UTC时区偏移的ISO8601字符串反序列化为Java 8 Instant?
有没有办法在所有场合配置Tomcat 7以创建带有安全标志的JSESSIONID cookie?
只有在通过https建立连接时,通常的配置才会导致Tomcat使用安全标记标记会话cookie.但是在我的生产场景中,Tomcat是一个反向代理/负载均衡器,它处理(并终止)https连接并通过http联系tomcat.
我可以以某种方式强制使用Tomcat在会话cookie上强制安全标志,即使连接是通过普通的http进行的吗?
想象一下以下场景:
class <T> Foo<T> {
....
}
class Bar {
Foo<Something> foo;
}
Run Code Online (Sandbox Code Playgroud)
我想为Foo编写一个自定义Jackson解串器.为了做到这一点(例如,为了反序列化Bar
具有类Foo<Something>
属性),我需要知道具体类型的Foo<T>
,在使用Bar
,在反序列化时间(比如我需要知道T
是Something
在particluar情况下).
如何编写这样的解串器?应该可以这样做,因为杰克逊用类型集合和地图来做.
澄清:
似乎有2个部分来解决问题:
1)获取foo
内部声明的属性类型Bar
并使用它来反序列化Foo<Somehting>
2)在反序列化时找出我们正在对foo
类内的属性进行反序列化Bar
以便成功完成步骤1)
如何完成1和2?
Mockito在模拟client.getPrograms()
哪些应该返回时报告未完成的存根错误SortedSet<Program>
.有趣的是,它仅在使用Java 7时才这样做,而在使用Java 6时则不然.
以下是模拟时触发错误的代码client.getPrograms()
:
private void prepareScheduleChangePreconditions() {
Client client = mock(Client.class);
TimeTable tt = BuilderUtil.buildTable(AcceleratedScheduleTimeTable.Schedule.NORMAL, "08:00");
when(clientRepository.findByCode(anyString())).thenReturn(client);
//Error is reported for next line of code
when(client.getPrograms()).thenReturn(new TreeSet<Program>(Collections.singleton(program)));
when(event.getTimeTable()).thenReturn(tt);
}
Run Code Online (Sandbox Code Playgroud)
这是错误输出:
Tests in error:
testExampleScheduleChangeNotify1(com.example.service.impl.ExampleServiceImplTest):
Unfinished stubbing detected here:
-> at com.example.service.impl.ExampleServiceImplTest.prepareScheduleChangePreconditions(ExampleServiceImplTest.java:134)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
Run Code Online (Sandbox Code Playgroud)
方法不是最终的.任何帮助或线索将不胜感激.
更新1
根据Mike B的请求,我设法将其分解为在Java …
我正在使用Spring Boot和Jetty,我似乎无法排除Gradle构建文件中的所有Tomcat依赖项.
build.gradle的相关部分:
compile("org.springframework.boot:spring-boot-starter") {
exclude module: "tomcat-embed-el"
}
compile("org.springframework.boot:spring-boot-starter-jetty")
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
Run Code Online (Sandbox Code Playgroud)
然而,当我运行gradle dependencies
部分tomcat仍然存在,并导致WebSockets问题:
...
|
+--- org.springframework.boot:spring-boot-starter-web: -> 1.4.1.RELEASE
| +--- org.springframework.boot:spring-boot-starter:1.4.1.RELEASE (*)
| +--- org.hibernate:hibernate-validator:5.2.4.Final
| | +--- javax.validation:validation-api:1.1.0.Final
| | +--- org.jboss.logging:jboss-logging:3.2.1.Final -> 3.3.0.Final
| | \--- com.fasterxml:classmate:1.1.0 -> 1.3.1
| +--- com.fasterxml.jackson.core:jackson-databind:2.8.3
| | +--- com.fasterxml.jackson.core:jackson-annotations:2.8.0 -> 2.8.3
| | \--- com.fasterxml.jackson.core:jackson-core:2.8.3
| +--- org.springframework:spring-web:4.3.3.RELEASE
| | +--- org.springframework:spring-aop:4.3.3.RELEASE (*)
| | +--- org.springframework:spring-beans:4.3.3.RELEASE (*)
| | +--- org.springframework:spring-context:4.3.3.RELEASE (*) …
Run Code Online (Sandbox Code Playgroud) 我正在play框架中编写一个控制器方法,该方法调用一个返回Future的函数,该函数也可能抛出异常.我无法弄清楚如何捕获和处理该异常.
这是我试过的:
def openIDCallback = Action { implicit request =>
Async (
Try(OpenID.verifiedId) match {
case Failure(thrown) => {
PurePromise(Ok("failed: " + thrown))
}
case Success(successResult) => {
successResult.map( userInfo => {
Ok(userInfo.id + "\n" + userInfo.attributes)
})
}
}
)
}
Run Code Online (Sandbox Code Playgroud)
OpenID.verifiedId是来自Play的OpenId api的函数,它返回Future [UserInfo].这是该函数的来源:
def verifiedId(queryString: Map[String, Seq[String]]): Future[UserInfo] = {
(queryString.get("openid.mode").flatMap(_.headOption),
queryString.get("openid.claimed_id").flatMap(_.headOption)) match { // The Claimed Identifier. "openid.claimed_id" and "openid.identity" SHALL be either both present or both absent.
case (Some("id_res"), Some(id)) => {
// MUST perform discovery …
Run Code Online (Sandbox Code Playgroud) 我想获得对变量的运行时KClass的引用.我浏览了有关类和反射的文档,但文档似乎只解释了如何获取对KClass的静态引用(例如String::class
for String
)
我需要一个变量的运行时KClass.这似乎没有编译:
fun test(x: Any) {
val klazz = x::class
}
Run Code Online (Sandbox Code Playgroud)
怎样才能获得KClass
的x
在上面的例子?
Kotlin中的注释可以有不同的使用地点目标,如下所述:https://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets
我的问题是:当没有明确定义use-site时,在类中注释属性时的默认目标是什么,如下例所示?
class Test {
@SomeAnnotation
var someProperty: String? = null
}
Run Code Online (Sandbox Code Playgroud)
背景
我正在尝试将Jongo作为Kotlin中的MongoDB客户端,并且在注释id字段时遇到问题.当它注释时,Jongo没有正确映射id属性:
@MongoId @MongoObjectId var id: String? = null
Run Code Online (Sandbox Code Playgroud)
有问题的注释只是杰克逊的元注释.但是,当我像这样注释属性时,它似乎工作,表明使用现场问题:
@field:[MongoId MongoObjectId]
var id: String? = null
Run Code Online (Sandbox Code Playgroud)
我希望这@field
是默认的使用网站,但似乎不是.