小编Seb*_*Seb的帖子

如何记录Spring 5 WebClient调用

我正在尝试使用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)

java logging spring-boot spring-webflux

25
推荐指数
11
解决办法
2万
查看次数

为什么Spring数据存储库上的getOne(...)不会抛出EntityNotFoundException?

我正在处理一个奇怪的问题,我正在进行集成测试,调用我的控制器从数据库中获取一个不存在的对象.

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并且我返回一个可选的这个奇怪的实体.

我无法理解这种行为.

java spring jpa spring-data spring-data-jpa

23
推荐指数
2
解决办法
2万
查看次数

Spring 5 webflux如何在Webclient上设置超时

我正在尝试在我的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

spring reactor reactor-netty spring-webflux

15
推荐指数
4
解决办法
1万
查看次数

使用ssl的Spring 5 WebClient

我正在尝试查找WebClient使用的示例.我的目标是使用Spring 5 WebClient使用https和自签名证书查询REST服务

任何例子?

ssl spring self-signed reactive-programming spring-webflux

14
推荐指数
3
解决办法
1万
查看次数

Spring环境下,我在新项目中还应该使用Hystrix吗?

看来Hystrix 的生命周期已接近尾声,而 Netflix 堆栈现在已经有点过时了。

我们正在为一个全新的项目构建一个堆栈,我们需要一个断路器,我们的默认选择是 Hystrix,因为它是众所周知的并且受到团队的赞赏。

如今Hystrix已完全集成到Spring Cloud中,是否有计划很快将其删除?

spring spring-boot hystrix spring-cloud-netflix

10
推荐指数
3
解决办法
9995
查看次数

如何停止/启动/暂停@JmsListener(干净的方式)

我在我的项目上使用Spring(启动)并使用以下命令访问JMS队列(ActiveMQ):

@JmsListener(destination = "mydestinationQueue")
public void processMessage(String content) {
    //do something
}
Run Code Online (Sandbox Code Playgroud)

它运行完美,但我需要能够以编程方式停止/暂停/启动此bean(REST调用或类似的东西)

当我停止或暂停这个bean时,我想确保完全处理当前消息.

有什么想法吗?

谢谢

jms spring-jms message-listener spring-boot

9
推荐指数
2
解决办法
8720
查看次数

如何使用"Spring Data JPA"规范进行单元测试方法

我正在玩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)

java spring unit-testing spring-data spring-data-jpa

7
推荐指数
2
解决办法
8437
查看次数

Spring rest模板readTimeOut

我正在尝试理解restTemplate上可用的readTimeout,它究竟是什么?

它是在我们获得超时异常之前请求可以花费的总时间吗?

spring spring-boot spring-rest

6
推荐指数
3
解决办法
1万
查看次数

使用 WebTestClient 和 ControllerAdvice 单元测试弹簧控制器

我正在尝试对我的控制器和特定情况进行单元测试:我的服务返回一个 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)

junit spring-test spring-restcontroller spring-webflux

5
推荐指数
1
解决办法
3394
查看次数

在 Angular 4.4 应用程序中获取并加载远程组件/模块

这是我正在尝试实现的目标:我想使用“插件”创建一个应用程序,我所说的插件是:

另一个可以部署在远程主机上并显示在我的应用程序中的角度组件/模块。

看来我不能直接使用 webpack 做这样的事情,我应该使用 SystemJs 来动态加载模块。

欢迎使用 SystemJs 和 Webpack (ng cli) 提供任何建议,以及如何调用远程模块并加载它们的示例。

plugins systemjs angular

5
推荐指数
1
解决办法
6613
查看次数