gpt4 book ai didi

java - java 类的隐式 jsonFormat

转载 作者:行者123 更新时间:2023-12-01 22:28:17 25 4
gpt4 key购买 nike

我有一个生成的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com