我正在尝试使用Spring 5 WebClient记录请求.你知道我怎么能实现这个目标吗?
(我使用的是Spring 5和Spring boot 2)
代码现在看起来像这样:
try {
return webClient.get().uri(url, urlParams).exchange().flatMap(response -> response.bodyToMono(Test.class))
.map(test -> xxx.set(test));
} catch (RestClientException e) {
log.error("Cannot get counter from opus", e);
throw e;
}
Run Code Online (Sandbox Code Playgroud) 我正在处理一个奇怪的问题,我正在进行集成测试,调用我的控制器从数据库中获取一个不存在的对象.
public Optional<T> get(Long id) {
try {
return Optional.ofNullable(repository.getOne(id));
} catch(EntityNotFoundException e) {
return Optional.empty();
}
}
Run Code Online (Sandbox Code Playgroud)
什么时候getOne(…)
找不到什么东西,我一直在期待,EntityNotFoundException
但实际上什么都没有.如果我检查我的结果,我可以看到我有一个空实体,其中有一个处理程序链接"扔EntityNotFoundException
"但我们没有进入catch并且我返回一个可选的这个奇怪的实体.
我无法理解这种行为.
我正在尝试在我的WebClient上设置超时,这是当前代码:
SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
ClientHttpConnector httpConnector = new ReactorClientHttpConnector(opt -> {
opt.sslContext(sslContext);
HttpClientOptions option = HttpClientOptions.builder().build();
opt.from(option);
});
return WebClient.builder().clientConnector(httpConnector).defaultHeader("Authorization", xxxx)
.baseUrl(this.opusConfig.getBaseURL()).build();
Run Code Online (Sandbox Code Playgroud)
我需要添加超时和汇集策略,我正在考虑这样的事情:
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(this.applicationConfig.getHttpClientMaxPoolSize());
cm.setDefaultMaxPerRoute(this.applicationConfig.getHttpClientMaxPoolSize());
cm.closeIdleConnections(this.applicationConfig.getServerIdleTimeout(), TimeUnit.MILLISECONDS);
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(this.applicationConfig.getHttpClientSocketTimeout())
.setConnectTimeout(this.applicationConfig.getHttpClientConnectTimeout())
.setConnectionRequestTimeout(this.applicationConfig.getHttpClientRequestTimeout()).build();
CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(cm).build();
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何在我的webclient中设置httpClient
我正在尝试查找WebClient使用的示例.我的目标是使用Spring 5 WebClient使用https和自签名证书查询REST服务
任何例子?
看来Hystrix 的生命周期已接近尾声,而 Netflix 堆栈现在已经有点过时了。
我们正在为一个全新的项目构建一个堆栈,我们需要一个断路器,我们的默认选择是 Hystrix,因为它是众所周知的并且受到团队的赞赏。
如今Hystrix已完全集成到Spring Cloud中,是否有计划很快将其删除?
我在我的项目上使用Spring(启动)并使用以下命令访问JMS队列(ActiveMQ):
@JmsListener(destination = "mydestinationQueue")
public void processMessage(String content) {
//do something
}
Run Code Online (Sandbox Code Playgroud)
它运行完美,但我需要能够以编程方式停止/暂停/启动此bean(REST调用或类似的东西)
当我停止或暂停这个bean时,我想确保完全处理当前消息.
有什么想法吗?
谢谢
我正在玩org.springframework.data.jpa.domain.Specifications,它只是一个基本的搜索:
public Optional<List<Article>> rechercheArticle(String code, String libelle) {
List<Article> result = null;
if(StringUtils.isNotEmpty(code) && StringUtils.isNotEmpty(libelle)){
result = articleRepository.findAll(Specifications.where(ArticleSpecifications.egaliteCode(code)).and(ArticleSpecifications.egaliteLibelle(libelle)));
}else{
if(StringUtils.isNotEmpty(code)){
result= articleRepository.findAll(Specifications.where(ArticleSpecifications.egaliteCode(code)));
}else{
result = articleRepository.findAll(Specifications.where(ArticleSpecifications.egaliteLibelle(libelle)));
}
}
if(result.isEmpty()){
return Optional.empty();
}else{
return Optional.of(result);
}
}
Run Code Online (Sandbox Code Playgroud)
这实际上工作正常,但我想为这个方法编写单元测试,我无法弄清楚如何检查传递给我的articleRepository.findAll()的规范
目前我的单元测试看起来像:
@Test
public void rechercheArticle_okTousCriteres() throws FacturationServiceException {
String code = "code";
String libelle = "libelle";
List<Article> articles = new ArrayList<>();
Article a1 = new Article();
articles.add(a1);
Mockito.when(articleRepository.findAll(Mockito.any(Specifications.class))).thenReturn(articles);
Optional<List<Article>> result = articleManager.rechercheArticle(code, libelle);
Assert.assertTrue(result.isPresent());
//ArgumentCaptor<Specifications> argument = ArgumentCaptor.forClass(Specifications.class);
Mockito.verify(articleRepository).findAll(Specifications.where(ArticleSpecifications.egaliteCode(code)).and(ArticleSpecifications.egaliteLibelle(libelle)));
//argument.getValue().toPredicate(root, query, builder);
} …
Run Code Online (Sandbox Code Playgroud) 我正在尝试理解restTemplate上可用的readTimeout,它究竟是什么?
它是在我们获得超时异常之前请求可以花费的总时间吗?
我正在尝试对我的控制器和特定情况进行单元测试:我的服务返回一个 Mono.Empty,我抛出一个 NotFoundException 并且我不想确保我收到 404 异常
这是我的控制器:
@GetMapping(path = "/{id}")
public Mono<MyObject<JsonNode>> getFragmentById(@PathVariable(value = "id") String id) throws NotFoundException {
return this.myService.getObject(id, JsonNode.class).switchIfEmpty(Mono.error(new NotFoundException()));
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器建议:
@ControllerAdvice
public class RestResponseEntityExceptionHandler {
@ExceptionHandler(value = { NotFoundException.class })
protected ResponseEntity<String> handleNotFound(SaveActionException ex, WebRequest request) {
String bodyOfResponse = "This should be application specific";
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Resource not found");
}
}
Run Code Online (Sandbox Code Playgroud)
和我的测试:
@Before
public void setup() {
client = WebTestClient.bindToController(new MyController()).controllerAdvice(new RestResponseEntityExceptionHandler()).build();
}
@Test
public void assert_404() throws Exception {
when(myService.getobject("id", JsonNode.class)).thenReturn(Mono.empty());
WebTestClient.ResponseSpec response …
Run Code Online (Sandbox Code Playgroud) 这是我正在尝试实现的目标:我想使用“插件”创建一个应用程序,我所说的插件是:
另一个可以部署在远程主机上并显示在我的应用程序中的角度组件/模块。
看来我不能直接使用 webpack 做这样的事情,我应该使用 SystemJs 来动态加载模块。
欢迎使用 SystemJs 和 Webpack (ng cli) 提供任何建议,以及如何调用远程模块并加载它们的示例。
spring ×6
spring-boot ×4
java ×3
spring-data ×2
angular ×1
hystrix ×1
jms ×1
jpa ×1
junit ×1
logging ×1
plugins ×1
reactor ×1
self-signed ×1
spring-jms ×1
spring-rest ×1
spring-test ×1
ssl ×1
systemjs ×1
unit-testing ×1