小编Phi*_*rdi的帖子

从Java 8中的Method返回Lambda?

我刚刚开始使用Java 8,我想知道是否有办法编写一个返回的方法Function

现在我的方法如下:

Function<Integer, String> getMyFunction() {
  return new Function<Integer, String>() {
    @Override public String apply(Integer integer) {
      return "Hello, world!"
    }
  } 
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在Java 8中更简洁地编写?我希望这会起作用,但它不会:

Function<Integer, String> getMyFunction() {
  return (it) -> { return "Hello, world: " + it } 
}
Run Code Online (Sandbox Code Playgroud)

java lambda java-8

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

Maven SCM插件:找不到Git SSH提供程序

我在使用Git的Maven SCM插件时遇到问题.我无法让插件工作,因为它说找不到提供程序.我运行时出现以下错误mvn scm:tag:

[错误]无法在项目上执行目标org.apache.maven.plugins:maven-scm-plugin:1.9:tag(default-cli)hello-world-service-minimal:无法运行标记命令:无法加载scm供应商.没有这样的提供者:'git:ssh://git@git-eng.REDACTED.com'. - > [帮助1]

我的pom.xml如下所示:

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>net.REDACTED</groupId>
  <artifactId>hello-world-service-minimal</artifactId>
  <version>1.0.13</version>
  <packaging>pom</packaging>

  <name>hello-world-service</name>

  <properties>
     <lang.java.source>1.7</lang.java.source>
     <lang.java.target>1.7</lang.java.target>

    <dep.junit>4.11</dep.junit>
  </properties>

  <scm>
     <developerConnection>scm:git:ssh://git@git-eng.REDACTED.com|PROJECT_NAME/hello-world-service-minimal.git</developerConnection>
     <url>scm:git:http://git-eng.REDACTED.com/PROJECT_NAME/hello-world-service-minimal/tree/master</url>
  </scm>

  <distributionManagement>
     <repository>
        <id>dev.release</id>
        <url>file:${project.build.directory}/repository/</url>
     </repository>
  </distributionManagement>

  <build>
      <plugins>
          <plugin>
              <groupId>org.codehaus.mojo</groupId>
              <artifactId>versions-maven-plugin</artifactId>
              <version>2.1</version>
          </plugin>
          <plugin>
              <artifactId>maven-scm-plugin</artifactId>
              <version>1.9</version>
              <configuration>
                  <tag>${project.artifactId}-${project.version}</tag>
              </configuration>
          </plugin>
      </plugins>
  </build>
</project>
Run Code Online (Sandbox Code Playgroud)

任何人都知道如何解决这个问题?这真让我抓狂.我根本无法弄清楚我做错了什么.

maven-scm maven

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

Hibernate Validator和Jackson:使用@JsonProperty值作为ConstraintViolation PropertyPath?

假设我有一个简单的POJO,如下面注释了Jackson 2.1和Hibernate Validator 4.3.1注释:

final public class Person {
  @JsonProperty("nm")
  @NotNull
  final public String name;

  public Person(String name) {
      this.name = name;
  }
}
Run Code Online (Sandbox Code Playgroud)

我将这样的JSON发送到Web服务:

{"name": null}
Run Code Online (Sandbox Code Playgroud)

报告ConstraintViolation时,Hibernate使用类成员标识符"name"而不是JsonProperty注释值.有谁知道是否可以让Hibernate Validator查看类的注释并使用该值代替?

hibernate-validator jackson bean-validation

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

Keep Ansible DRY:如何避免重复变量?

最近刚开始使用Ansible,我遇到了一个问题.在我的一个YAML结构中,我定义了这样的东西:

---
# file: main.yml
#
# Jenkins variables for installation and configuration

jenkins:
  debian:
    # Debian repository containing Jenkins and the associated key for accessing the repository
    repo: 'deb http://pkg.jenkins-ci.org/debian binary/'
    repo_key: 'http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key'

    # Dependencies needed for Jenkins to run properly
    dependencies:
      - 'openjdk-7-jdk'
      - 'git-core'
      - 'curl'

  port: 8080

  # Installation directory
  home: '/opt/jenkins'

  # Path to the location of the main Jenkins-cli binary
  bin_path: '{{jenkins.home}}/jenkins-cli.jar'

  # Path to the location of the Jenkins updates file
  updates_path: '{{jenkins.home}}/updates_jenkins.json' …
Run Code Online (Sandbox Code Playgroud)

ansible ansible-playbook

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

杰克逊基于对象属性反序列化为POJO

是否可以使用Jackson将JSON反序列化为基于JSON内容的两种类型之一?

例如,我有以下Java(技术上是Groovy,但这并不重要)接口和类:

interface Id {
    Thing toThing() 
}

class NaturalId implements Id {

    final String packageId

    final String thingId

    Thing toThing() {
        new PackageIdentifiedThing(packageId, thingId)
    }         
}

class AlternateId implements Id {

    final String key

    Thing toThing() {
        new AlternatelyIdentifiedThing(key)
    }
}
Run Code Online (Sandbox Code Playgroud)

我将收到的JSON将如下所示:

此JSON应映射到NaturalId {"packageId": "SomePackage", "thingId": "SomeEntity"}

此JSON应映射到AlternateId {"key": "SomeUniqueKey"}

有没有人知道我怎么能用Jackson 2.x完成这个,而不包括类型id?

java json jaxb jackson

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

使用 Kubernetes client-go 如何以编程方式检查节点是否“就绪”?

看来我可能需要遍历v1.Node->NodeStatus->Conditions[]切片并按转换时间排序,并查找最近的计时条件是否为NodeConditionType == "Ready"。我想知道是否有更好的方法或者这种方法是否有缺陷?

kubernetes client-go

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

在Python中同时混合多个类?

有谁知道如何让下面的代码工作?

def mixin(TargetClass, *args, **kwargs):
    """*args is a bunch of classes to mixin to the TargetClass"""
    if kwargs.get('name') is None:
        kwargs['name'] = '%s_mixed_with_%s' % (TargetClass.__name__, "".join(map(str, args)))

    class MixedClass(TargetClass, *args):
        pass

    MixedClass.__name__ = kwargs.get('name')
    return MixedClass

# assume for all intents and purposes that Foo, Bar, Baz, Bot, Quux, and Muck are Classes
# Foo should inherit from all of Bar, Baz, Bot, Quux, and Muck
def uber_foo = mixin(Foo, Bar, Baz, Bot, Quux, Muck, name="UberFoo")`
Run Code Online (Sandbox Code Playgroud)

现在Python解释器(2.7.5)引发了以下错误:

TypeError:调用元类基类元类冲突时出错:派生类的元类必须是其所有基类的元类的(非严格)子类

python mixins

2
推荐指数
1
解决办法
248
查看次数

杰克逊 - 将值传递给JsonDeserializer

我有一个现有的类层次结构,如下所示:

public interface Service {
  String getId();
  String getName();
}

public class FooTask extends AbstractTask {
  private final static ObjectMapper JSON_MAPPER = new ObjectMapper();

  static {
    JSON_MAPPER.registerModule(new SimpleModule().addDeserializer(Result.class, new ResultDeserializer());
  }

  public FooTask(Service service) {
    super(service);
  }

  @Override public Result call() throws Exception {
     InputStream json = <... execute some code to retrieve JSON ...>
     Result result = JSON_MAPPER.readValue(json, Result.class);
  }

  private static class ResultDeserializer {
    @Override public Result deserialize(JsonParser parser, DeserializationContext ctx) throws IOException {

      //
      // Need to …
Run Code Online (Sandbox Code Playgroud)

java json jackson

2
推荐指数
1
解决办法
2126
查看次数