小编HoT*_*icE的帖子

@JsonIgnore在Scala案例类中不起作用

我有一个简单的案例课

case class Project(@JsonIgnore id: Option[UUID], name: Option[String])
Run Code Online (Sandbox Code Playgroud)

我正在使用com.fasterxml.jackson

com.fasterxml.jackson.core:jackson-databind:2.8.4
com.fasterxml.jackson.core:jackson-annotations:2.8.4
org.skinny-framework.com.fasterxml.jackson.module:jackson-module-scala_2.12:2.8.4
Run Code Online (Sandbox Code Playgroud)

...

private val mapper = new ObjectMapper()
mapper.registerModule(DefaultScalaModule)
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
mapper.writeValueAsString(project) 
Run Code Online (Sandbox Code Playgroud)

尽管@JsonIgnore将ID写入结果json

我究竟做错了什么?

更新:

当前的解决方法:

@JsonIgnoreProperties(Array("id"))
case class Project(id: Option[UUID], name: Option[String]) 
Run Code Online (Sandbox Code Playgroud)

这很好用:)

json scala

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

Java Akka Spring扩展子演员麻烦

我有这个Akka Spring扩展名:

    // ...    
    @Service("springExtension")
    public class SpringExtension implements Extension {

        @Autowired
        private ApplicationContext applicationContext;

        public Props props(String actorBeanName) {
            return Props.create(SpringActorProducer.class,
                    applicationContext, actorBeanName);
        }

        public Props props(String actorBeanName, Object... args) {
            return (args != null && args.length > 0)
                    ? Props.create(SpringActorProducer.class, applicationContext, actorBeanName, args)
                    : props(actorBeanName);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这个演员制片人:

    // ...    
    public class SpringActorProducer implements IndirectActorProducer {

        final ApplicationContext applicationContext;
        final String actorBeanName;
        final Object[] args;

        public SpringActorProducer(ApplicationContext applicationContext, String actorBeanName) {
            this.applicationContext = applicationContext;
            this.actorBeanName = actorBeanName;
            this.args …
Run Code Online (Sandbox Code Playgroud)

java spring akka

5
推荐指数
0
解决办法
296
查看次数

从不同类中运行常用函数的最佳方法

我有这门课

public abstract class Foo {
    def execute: Unit = ???
}

public abstract class Bar {
    def execute: Unit = ???
}

public class FooFoo extends Foo {
    def execute: Unit = ???
}

public class BarBar extends Bar {
    def execute: Unit = ???
}
Run Code Online (Sandbox Code Playgroud)

在某些我有这种方法的地方:

def executeSomething(body: => AnyRef) : = Try(body) match ...
Run Code Online (Sandbox Code Playgroud)

电话看起来像这样

x match {
  case _: Foo => executeSomething(x.execute)
  case _: Bar => executeSomething(x.execute)
}
Run Code Online (Sandbox Code Playgroud)

是否有某种方式,我可以这样做(没有新的类)

val u = executeSomething(x)
Run Code Online (Sandbox Code Playgroud)

UPD

对不起大家.这个mb实际代码会更清晰 …

types functional-programming scala

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

不使用val存储值

假设有3个数字:

val x = 10
val y = 5
val z = 14
Run Code Online (Sandbox Code Playgroud)

我们想要做一些逻辑:

if (x + y > z) {
  println(x + y)
} else if (x + y < z) {
  println(-1)
} else {
  println(0)
}
Run Code Online (Sandbox Code Playgroud)

如果我们的"z + y"操作很昂贵,我们必须完全计算一次:

val sum = x + y

if (sum > z) {
  println(sum)
} else if (sum < z) {
  println(-1)
} else {
  println(0)
}
Run Code Online (Sandbox Code Playgroud)

但我想要更多的功能方式:

if (x + y > z) => sum { //This is will …
Run Code Online (Sandbox Code Playgroud)

functional-programming scala

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

PartialFunction隐式参数

我有一个简单的PartialFunction

type ChildMatch = PartialFunction[Option[ActorRef], Unit]

def idMatch(msg: AnyRef, fail: AnyRef)(implicit ctx: ActorContext): ChildMatch = {
    case Some(ref) => ref forward msg
    case _ => ctx.sender() ! fail
}
Run Code Online (Sandbox Code Playgroud)

但是当我试图使用它时 - 编译器需要这样的声明:

...
implicit val ctx: ActorContext
val id: String = msg.id

idMatch(msg, fail)(ctx)(ctx.child(id))
Run Code Online (Sandbox Code Playgroud)

你可以看到它想要ctx作为第二个参数而不是隐含的

我怎么能改变我的idMatch函数使用它像这样:

...
implicit val ctx: ActorContext
val id: String = msg.id

idMatch(msg, fail)(ctx.child(id))
Run Code Online (Sandbox Code Playgroud)

scala implicit akka partial-functions

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

Scala中功能的简单组合

我有我的代码的简化版本。显而易见,我在概念上想要什么:

def heavyCalcMul: Int => Int = i => i * 2
def heavyCalcDiv: Int => Int = i => i / 2
def heavyCalcPls: Int => Int = i => i + 2
Run Code Online (Sandbox Code Playgroud)

我这样使用它:

val x = 2
val midResult = heavyCalcMul(x)
val result = heavyCalcDiv(midResult) + heavyCalcPls(midResult)
Run Code Online (Sandbox Code Playgroud)

但是我想用这种风格重写这段代码:

val x = 2
val result = heavyCalcMul(x) { midResult: Int =>
  heavyCalcDiv(midResult) + heavyCalcPls(midResult)
}
Run Code Online (Sandbox Code Playgroud)

可能吗?

scala function-composition scalastyle

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

如何在没有return语句的情况下编写这个lambda表达式?

以下方法实现了一个BiFunctiona Map<String,String>和一个值来搜索.它搜索的EntryMap包含给定值,并返回相应的键.

这个实现有效,但是我想写一个没有return语句的lambda表达式,以使代码更优雅.

private BiFunction<Map<String, String>, String, String> findName = (m, s) -> {
    Map.Entry<String, String> e = 
        m.entrySet()
         .stream()
         .filter(entry -> entry.getValue() != null && !entry.getValue().isEmpty() && entry.getValue().equals(s))
         .findFirst()
         .orElse(null);
    return e != null ? e.getKey() : null;
};
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

java lambda optional java-8

-1
推荐指数
1
解决办法
201
查看次数