java - java 类的隐式 jsonFormat

标签 java json scala akka spray

我有一个生成的java类:

package cc.notsoclever.customerservice
public class Customer {
  protected String name;
  public String getName() {
    return name;
  }
  public void setName(String value) {
    this.name = value;
  }
}

(从 WSDL 生成以提供 SOAP 接口(interface),但这并不重要)

我想连接到 JSON 接口(interface),所以我需要一个隐式 jsonFormat - 我已经尝试过:

package cc.notsoclever.customerservice

object CustomerProtocol {
  import spray.json._, spray.json.DefaultJsonProtocol._
  object Customer {
    implicit val format = jsonFormat(Customer.apply, "name")
  }
}

但它导致了错误:

...value apply is not a member of object cc.notsoclever.customerservice.CustomerProtocol.Customer
[error]  Note: implicit value format is not applicable here because it comes after the application point and it lacks an explicit result type
[error]     implicit val format = jsonFormat(Customer.apply, "name")
[error]                                               ^

是否可以定义使用公共(public) getter 和 setter 的隐式格式?或者我需要手动定义编码吗?

最佳答案

Spray 的 jsonFormatX 帮助器适用于案例类。您可以编写手动格式或使用面向 Java Bean 的 Jackson 等库。

关于java - java 类的隐式 jsonFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28304236/

相关文章:

ios - 注册问题上的 JSON 数据处理

java - 如何在非 TabActivity 的 Activity 中创建 tabHost

java - 在 Joda Time 的帮助下使用夏令时的正确方法

python - 是否可以在不将编码器传递给 json.dumps() 的情况下将枚举转储到 json 中?

java - Jackson 中的通用元组反序列化

scala - Scala 集合上的高效分组聚合

scala - Play Framework 路由和scala predef值

ScalaTest 相当于 JUnit Rule 临时文件夹?

java - Java中如何覆盖子类中Super对象的某个字段?

java - 为什么这段代码线程安全?