我正在使用Spring REST控制器,特别是在异常处理程序上。异常处理程序按预期工作,并且我的JUnit-Test(使用Spring HTTP客户端)显示在客户端接收到正确的HTTP状态代码(400)。HTTP客户端会自动将其转换为HttpClientErrorException。
但是,HttpClientErrorException始终打印总是为我产生以下结果:
HttpClientErrorException: 400 null
... null部分是让我担心的。这不是服务器端异常的消息应该存在的地方吗?
我检查了HTTP客户端的源代码,以查看客户端异常在何处引发。看起来像这样:
throw new HttpClientErrorException(statusCode, response.getStatusText(), response.getHeaders(), getResponseBody(response), getCharset(response));
调试此调用后发现这response.getStatusText()就是null我的情况。
我的问题是:如何ResponseEntity在服务器端进行设计,以使HTTP客户端找到服务器端的异常消息response.getStatusText()而不是null?
我的当前看起来像这样:
@ExceptionHandler({ MyCustomException.class })
public ResponseEntity<String> handleException(final HttpServletRequest req, final MyCustomException e) {
HttpHeaders headers = new HttpHeaders();
headers.set("Content-type", "text/plain");
String body = e.toString();
return new ResponseEntity<>(body, headers, HttpStatus.BAD_REQUEST);
}
Run Code Online (Sandbox Code Playgroud)
...,然后输入null客户端状态文本。
我想构建一个工具,涉及从输入字符串解析 MongoDB 查询 AST。然而,我一直无法在网上找到任何全面的语法定义。
例如,有mnogodb-language-model ,但它已经非常过时了。它的语法不支持不带引号的表达式或正则表达式(以/.../符号表示),并且它的 AST 似乎不包含$expr过滤器内的任何内容。
我会很高兴收到任何最新的综合描述,(E)BNF、pegJS、ANTLR 等。有人知道我在哪里可以找到类似的东西吗?如果它恰好包含聚合管道语法,那就更好了。
我希望能够使用 Dagger 2 将依赖项注入 JUnit 测试(我是这个框架的新手)。从 Spring 开始,您可以执行以下操作:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MyApplication.class)
public class MyTestClass {
@Autowired
private MyService service;
@Test
public void testMySerivce() { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)
...但是对于 Dagger 2,我还没有找到不依赖于显式DaggerMyComponent.builder().build().myService().
理想情况下,我会想象解决方案如下所示:
// tell JUnit that dagger needs to do some post processing
@RunWith(DaggerJUnit4Runner.class)
// tell dagger which component classes to use for injection
@Components(MyComponent.class)
public class MyTestClass {
@Inject
private MyService service;
@Test
public void testMySerivce() { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,没有DaggerJunit4Runner。 …
ByteIterableXodus中的密钥和/或值是否有最大长度?如果存在硬限制,那么限制是多少(即多少字节)?如果ByteIterable超过这个限制会发生什么?
我正在开发一个同时使用 REST 端点和 SockJS websocket 的服务器应用程序。这曾经在 Spring 5.2 及更低版本下工作正常。
但是,从 5.3 版本开始,以下方法存在于 中org.springframework.web.cors.CorsConfiguration:
public void validateAllowCredentials() {
if (this.allowCredentials == Boolean.TRUE &&
this.allowedOrigins != null && this.allowedOrigins.contains(ALL)) {
throw new IllegalArgumentException(
"When allowCredentials is true, allowedOrigins cannot contain the special value \"*\"" +
"since that cannot be set on the \"Access-Control-Allow-Origin\" response header. " +
"To allow credentials to a set of origins, list them explicitly " +
"or consider using \"allowedOriginPatterns\" instead.");
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的套接字配置如下: …
我使用Spring Data JpaRepository,Hibernate作为JPA提供者.
通常直接与Hibernate之间的决策工作时EntityManager#persist(),并EntityManager#save()是由程序员.使用Spring Data存储库,只有save().我不想在这里讨论利弊.让我们考虑以下简单的基类:
@MappedSuperclass
public abstract class PersistableObject {
@Id
private String id;
public PersistableObject(){
this.id = UUID.randomUUID().toString();
}
// hashCode() and equals() are implemented based on equality of 'id'
}
Run Code Online (Sandbox Code Playgroud)
使用此基类,Spring Data存储库无法分辨哪些实体是"新的"(尚未保存到DB),因为id == null在这种情况下,定期检查 显然不起作用,因为UUID被热切地分配以确保正确性中equals()和hashCode().因此,存储库似乎总是调用EntityManager#merge()- 这对于瞬态实体来说显然效率低下.
问题是:我如何告诉JPA(或Spring Data)实体是新的,以便它使用EntityManager#persist()而不是#merge()在可能的情况下?
我正在考虑这些方面的东西(使用JPA生命周期回调):
@MappedSuperclass
public abstract class PersistableObject {
@Transient
private boolean isNew = true; // by default, treat entity as …Run Code Online (Sandbox Code Playgroud) 我正在使用番石榴Loading Cache来缓存HTTP请求的结果。Kotlin / KTOR提供了基于协程(即非阻塞HTTP请求)的HTTP客户端库。
我的问题是,加载缓存suspend不了解ing函数。该load功能我传递给加载缓存不能暂停。因此,我被迫在runBlocking调用中执行HTTP请求,从而完全消除了非阻塞调用的好处。
我的问题是:有没有更好的方法?您将如何实现协程结果的缓存?
我正在尝试热重新加载 Spring Boot 应用程序的内容安全策略(CSP)中的更改,即用户应该能够通过管理 UI 更改它,而无需重新启动服务器。
Spring Boot 中常规的做法是:
@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) {
// ... lots more config here...
http.headers()
.addHeaderWriter(
StaticHeadersWriter(
"Content-Security-Policy",
"<some policy string>"
)
)
}
}
Run Code Online (Sandbox Code Playgroud)
...但这不允许在分配后重新配置。
我可以在运行时(重新)配置它吗?重新加载应用程序上下文不是一个选项,我只需要能够适应这个特定的设置。
我使用 Lucene 5.5.0 进行索引。以下标准描述了我的环境:
String或Long字段(因此不需要文本分析)。所有这些都由lucene存储。字符串的最大长度为 255 个字符。search我实现的当前方法,包装了 Lucene 的 API,如下所示:
public Set<Document> performLuceneSearch(Query query) {
Set<Document> documents = Sets.newHashSet();
// the reader instance is reused as often as possible, and exchanged
// when a write occurs using DirectoryReader.openIfChanged(...).
if (this.reader.numDocs() > 0) {
// note that there cannot be a limiting number on the result set.
// …Run Code Online (Sandbox Code Playgroud) 我正在开发 Spring-Boot Web 应用程序。编写集成测试的通常方法是:
@Test
@Transactional
@Rollback(true)
public void myTest() {
// ...
}
Run Code Online (Sandbox Code Playgroud)
只要只有一个线程完成这项工作,这种方法就可以很好地工作。@Rollback如果有多个线程则无法工作。
然而,当@RestController使用 Spring REST 模板测试类时,总是有多个线程(按设计):
所以你不能@Rollback在 REST 测试中使用。问题是:您使用什么来使测试可重复并让它们在测试套件中正常运行?
@DirtiesContext可行,但这是一个糟糕的选择,因为在每个 REST 测试方法之后重新启动 Spring 应用程序上下文会使套件执行速度非常慢;每个测试需要几毫秒才能运行,但重新启动上下文需要几秒钟。
我正在处理一个沿多个边缘导航并最终生成String. 根据图形内容,此遍历可能为空。如果遍历最终为空,我想改为返回一个默认值。
这是我目前正在做的事情:
GraphTraversal<?, ?> traversal = g.traversal().V().
// ... fairly complex navigation here...
// eventually, we arrive at the target vertex and use its name
.values("name")
// as we don't know if the target vertex is present, lets add a default
.union(
identity(), // if we found something we want to keep it
constant("") // empty string is our default
)
// to make sure that we do not use the default if we have a value... …Run Code Online (Sandbox Code Playgroud) 我正在处理Java中的一个对象,它的计算成本非常高,而且大小只有几兆字节.为了在应用程序重新启动时保留它,我想将其序列化为a File,并在启动时重新加载该文件(如果存在).
问题是大多数文件系统不是事务性的.文件写入过程可能因异常,JVM终止和/或电源故障而中断.我绝对需要断言的是,如果使用该文件,则其中的信息是完整的.如果需要,我可以丢弃信息并重新计算,但必须避免阅读和依赖不完整的数据.
我的尝试是序列化并在文件末尾写一个"seal"对象,例如校验和.反序列化期间此对象的存在可确保序列化过程完成.如果在反序列化期间没有密封对象,我知道我不能信任文件中的数据,因为它可能是不完整的.我正在寻找一个独立于操作系统的解决方案,我不需要考虑恶意修改序列化文件内容的"攻击".
我的问题是:上面列出的密封对象方法是否安全,或者是否仍有一些极端情况下我最终会在不注意的情况下读取不完整的文件?
java ×8
spring ×5
rest ×3
spring-boot ×2
testing ×2
atomic ×1
caching ×1
cors ×1
dagger-2 ×1
ebnf ×1
filesystems ×1
grammar ×1
gremlin ×1
guava ×1
hibernate ×1
httpresponse ×1
jpa ×1
junit ×1
kotlin ×1
ktor ×1
lucene ×1
mongodb ×1
parsing ×1
performance ×1
search ×1
sockjs ×1
websocket ×1
xodus ×1