小编Goi*_*nas的帖子

用于自定义类的Spring @Value属性

是否可以使用Spring的@Value批注来读取和写入自定义类类型的属性值?

例如:

@Component
@PropertySource("classpath:/data.properties")
public class CustomerService {

    @Value("${data.isWaiting:#{false}}")
    private Boolean isWaiting;

    // is this possible for a custom class like Customer???
    // Something behind the scenes that converts Custom object to/from property file's string value via an ObjectFactory or something like that?
    @Value("${data.customer:#{null}}")
    private Customer customer;

    ...
}
Run Code Online (Sandbox Code Playgroud)

编辑解决方案

以下是我使用Spring 4.x API做到的方法......

为Customer类创建了新的PropertyEditorSupport类:

public class CustomerPropertiesEditor extends PropertyEditorSupport {

    // simple mapping class to convert Customer to String and vice-versa.
    private CustomerMap map;

    @Override
    public String getAsText() 
    {
        Customer …
Run Code Online (Sandbox Code Playgroud)

spring

9
推荐指数
1
解决办法
3707
查看次数

Flutter Web - http StreamedResponse 作为客户端接收 SSE

拥有一个 Flutter 应用程序,在移动设备上运行时能够接收 SSE 文本/事件流事件,但在 Chrome Web 上运行时无法接收相同的 SSE 文本/事件流事件。

\n\n
import \'package:flutter/material.dart\';\nimport \'package:http/http.dart\' as http;\n\nvoid main() => runApp(MyApp());\n\nclass MyApp extends StatelessWidget {\n\n  http.Client _client;\n\n  MyApp() : super() {\n    print("Ctor called");\n    subscribe();\n  }\n\n  @override\n  Widget build(BuildContext context) {\n    print("building..");\n    return MaterialApp(\n      title: \'Flutter SSE\',\n      home: Scaffold(\n        appBar: AppBar(\n          title: Text(\'Receive SSE Events\'),\n        ),\n        body: Center(\n          child: Text(\'Ready for events..\'),\n        ),\n      ),\n    );\n  }\n\n  subscribe() async {\n    print("Subscribing..");\n    try {\n      _client = http.Client();\n\n      var request = new http.Request("GET", Uri.parse("http://192.168.1.11:8080/myserver/events"));\n      request.headers["Cache-Control"] = …
Run Code Online (Sandbox Code Playgroud)

dart flutter

9
推荐指数
1
解决办法
2521
查看次数

使用HttpComponentsMessageSender的基于Auth的WebServiceTemplate

我正在尝试测试当前使用基本身份验证保护的Spring Web服务.对于这些测试,我使用Spring的WebServiceTemplate类编写了一个Web服务客户端.

我的Web服务客户端调用Web Service的工作没关系,当我创建模板的messageSender,和作为org.springframework.ws.transport.http.CommonsHttpMessageSender对象豆与org.apache.commons.httpclient.UsernamePasswordCredentials和,虽然客户端运行,代码有一个警告高亮说,CommonsHttpMessageSender类是现在已经过时,我应该使用HttpComponentsMessageSender来代替.

我已经尝试重新配置客户端WebServiceTemplate以使用较新的HttpComponentsMessageSender类工作,但我无法正确配置它的基本身份验证部分.对于新HttpComponentsMessageSender类,我使用org.apache.http.auth.UsernamePasswordCredentials该类创建了凭据,但是当我调用Web Service时,凭据似乎无法与请求一起使用?是否有一个WebServiceTemplate客户端的工作示例,它使用这些较新的类来验证请求等?

我的工作代码与旧的弃用类的Jars使用:commons-httpclient-3.1, spring-ws-core-2.2.0.RELEASE.

我不工作的代码与新类罐用途:httpclient-4.3.4,httpcore-4.3.2,spring-ws-core-2.2.0.RELEASE.

测试配置代表非工作代码:

package com.company.service.a.ws.test.config;

import java.io.IOException;

import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import …
Run Code Online (Sandbox Code Playgroud)

authentication spring web-services spring-ws spring-security

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

Kubernetes-Pod仍处于ContainerCreating状态

我对Kubernetes的所有事物都是陌生的,因此仍然有很多东西需要学习。

已经创建了两个节点的Kubernetes集群,并且两个节点(主节点和工作节点)都准备好进行工作,这很好:

[monkey@k8s-dp1 nginx-test]# kubectl get nodes
NAME      STATUS    ROLES     AGE       VERSION
k8s-dp1   Ready     master    2h        v1.9.1
k8s-dp2   Ready     <none>    2h        v1.9.1
Run Code Online (Sandbox Code Playgroud)

此外,所有Kubernetes Pod看起来都不错:

[monkey@k8s-dp1 nginx-test]# kubectl get pods --all-namespaces
NAMESPACE     NAME                              READY     STATUS    RESTARTS   AGE
kube-system   etcd-k8s-dp1                      1/1       Running   0          2h
kube-system   kube-apiserver-k8s-dp1            1/1       Running   0          2h
kube-system   kube-controller-manager-k8s-dp1   1/1       Running   0          2h
kube-system   kube-dns-86cc76f8d-9jh2w          3/3       Running   0          2h
kube-system   kube-proxy-65mtx                  1/1       Running   1          2h
kube-system   kube-proxy-wkkdm                  1/1       Running   0          2h
kube-system   kube-scheduler-k8s-dp1            1/1       Running   0          2h
kube-system   weave-net-6sbbn …
Run Code Online (Sandbox Code Playgroud)

kubernetes weave kubectl

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

Logstash Docker 映像 - 缺少 logstash 插件吗?

我们使用 Elastic 的 Logstash Docker 镜像 ( docker.elastic.co/logstash/logstash-oss:6.1.2) 作为我们自己的 Logstash Docker 镜像构建的基础,我们需要根据自己的需要包含几个 logstash 插件。但是,当我们查看下面的基本图像时,/opt/logstash/bin我们可以看到有logstash-plugin.bat文件但没有logstash-plugin.sh文件。此文件是否丢失,或者我们是否查看了错误的安装映像命令?

这是我们的 Dockerfile,它在构建时无法将给定的插件包含到新映像中:

FROM docker.elastic.co/logstash/logstash-oss:6.1.2

ENV LOGSTASH_HOME /opt/logstash
WORKDIR ${LOGSTASH_HOME}

RUN rm -f /usr/share/logstash/pipeline/logstash.conf \
  bin/logstash-plugin install logstash-input-kafka \
  bin/logstash-plugin install logstash-filter-prune

ADD pipeline/ /usr/share/logstash/pipeline/
ADD config/ /usr/share/logstash/config/
Run Code Online (Sandbox Code Playgroud)

我们应该如何安装基于 Elastic 的 Logstash Docker 镜像 v6.1.2 的 logstash 插件?

logstash docker

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

将更新的XML写入最初解析的文件

我有一个gradle.build我想要的地方:

  1. 读取XML文件
  2. 用于XmlSlurper更新读取XML文件中的属性
  3. 将更新的XML写回最初解析的xml文件.

第三步只有我写的修改XML的工作新的不存在的 XML文件,而不是最初解析XML文件.

将修改后的XML写入最初解析的XML文件的最简单方法是什么?


我的代码到目前为止:

def inFile = file('file.xml')
def outFile = file('_file.xml')

def xml = new XmlSlurper().parse(inFile)

// update xml code here

def outBuilder = new StreamingMarkupBuilder()
def outWriter = outFile.newWriter()
XmlUtil.serialize(outBuilder.bind{ mkp.yield xml }, outWriter)
Run Code Online (Sandbox Code Playgroud)

我想outFilefile.xml这样,它覆盖原始的XML文件.

groovy xmlslurper gradle

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

Spring Integration Java DSL - 如何使用重试建议调用ServiceActivator方法

我有一个带有ServiceActivator方法的Component类:

@Component("payloadService")
public class PayloadService {

    @Transactional
    @ServiceActivator
    @Description("Pre-check service")
    public Message<String> preCheck(Message<String> message) {
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个Spring Integration 4 Java DSL流程,它调用ServiceActivator的preCheck方法,如下所示:

IntegrationFlows.from("input.ch")
    .handle("payloadService", "preCheck")
    ...
    .get();
Run Code Online (Sandbox Code Playgroud)

我现在正在尝试向服务调用添加重试建议(如http://docs.spring.io/spring-integration/reference/htmlsingle/#retry-config所示)但我想以Java DSL形式执行此操作如https://github.com/spring-projects/spring-integration-extensions/wiki/Spring-Integration-Java-DSL-Reference#dsl-and-endpoint-configuration中所述.

但是我无法弄清楚如何在实践中将这个建议应用到我的DSL流程中.可能会挣扎,因为我还不太熟悉lambdas等.

有人可以给我一些关于如何做到这一点的指示吗?

PM,提前谢谢

spring spring-integration spring-dsl

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

log4j2 Web 查找不起作用

我们有基于 Spring java 的 Web 部署,它使用 log4j2.xml 将消息记录到文件等。

我们现在需要更新 log4j2.xml 配置,以便能够在其中执行 ${web:contextPath} Web 查找,以便我们可以使用部署的上下文名称作为记录器将消息记录到的日志文件名称的一部分。但是,当我们部署应用程序时,log4j2 配置无法识别任何与 Web 查找相关的内容。创建用于记录消息的文件只是使用名称创建的${web,并且实际上没有记录任何消息。

我已经阅读了与在 3.0 servlet 中运行时 log4j2 Web 查找相关的各种在线文档,但我仍然看不出我们的配置中可能存在什么问题。我不知道要在 log4j 的跟踪日志中查找什么,以便了解我们缺少什么。

我们的堆栈:

Windows and Linux OSes Java 8 Tomcat 7.0.5x log4j-xxx 2.2 (log4j-api, log4j-core, log4j-slf4j-impl, log4j-jcl, log4j-web all in classpath)

非常感谢任何有关如何使网络查找工作的帮助。

干杯,下午

java spring tomcat7 log4j2

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

找不到类型为:java.lang.Short的验证器

我有一个包含以下内容的JAXB生成的类:

@Pattern(regexp = "[0-9]", message = "Message details here")
protected Short msgVal;
Run Code Online (Sandbox Code Playgroud)

当我运行JUnit测试以通过Hibernate验证该类的实例时,我得到:

javax.validation.UnexpectedTypeException: No validator could be found for type: java.lang.Short.

如果我注释掉@Pattern约束,JUnitTest运行正常.

是否可以@Pattern对Short值进行约束?如果是这样,我错过了什么?

此致,PM.

java hibernate jaxb hibernate-validator

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

Spring Integration Java DSL - 带有@Header参数注释的@ServiceActivator方法

我有一个带有以下签名的 Spring Integration 4 bean 方法:

@Component
public class AService {

    @ServiceActivator
    public Message<?> serviceMethod(
            Message<?> message,
            @Header(ServiceHeader.A_STATE) AState state,
            @Header(ServiceHeader.A_ID) String id) {

        ...
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

目前,我从 Spring Integration Java DSL (spring-integration-java-dsl:1.0.1.RELEASE) 流程中调用此服务方法,如下所示:

.handle("aService", "serviceMethod")
Run Code Online (Sandbox Code Playgroud)

这工作得很好,但是,我想知道是否可以通过以下方式调用服务方法:

.handle(Message.class, (m, h) -> aService.serviceMethod(m, h))
Run Code Online (Sandbox Code Playgroud)

我想以这种方式调用服务方法的原因是,当有人使用 Eclipse 等 IDE 查看代码时,他们可以通过突出显示该方法并按 F3 等方式深入了解该服务的方法。

所以我的问题是,是否有另一种方法可以调用@ServiceActivator方法(包括@Header注释)而不使用字符串作为服务名称/服务方法.handle()

spring-integration java-8

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