gpt4 book ai didi

java - 将 ZonedDateTime 类型转换为 Gson

转载 作者:搜寻专家 更新时间:2023-10-31 08:16:36 24 4
gpt4 key购买 nike

我有返回对象数组列表的休息服务,我已经实现了 jersy restful 客户端来执行它,但是我在将 ZonedDateTime 类型转换为 json 时遇到问题所以我得到这个错误

 Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 231 path $[0].lastmodifieddate

我该如何解决这个问题?

实体中的 lastmodifiedate 列

 @Column(name = "lastmodifieddate")
private ZonedDateTime lastmodifieddate;

//getter and setter

休息服务

@RequestMapping(value = "/getScoreFactor",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<Scorefactor> getScoreFactor() throws JSONException {
return scoreService.getScoreFactor();
}

jersy restful 客户端

  try {

Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/adap/api/getScoreFactor");
ClientResponse response = webResource.accept("application/json")
.get(ClientResponse.class);

String output = response.getEntity(String.class);

System.out.println("output--"+output);
Type listType = new TypeToken<List<Scorefactor>>() {}.getType();

List<Scorefactor> scorefactors = new Gson().fromJson(output,listType);

System.out.println(scorefactors);

} catch (Exception e) {

e.printStackTrace();

}

最佳答案

public static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(ZonedDateTime.class, new TypeAdapter<ZonedDateTime>() {
@Override
public void write(JsonWriter out, ZonedDateTime value) throws IOException {
out.value(value.toString());
}

@Override
public ZonedDateTime read(JsonReader in) throws IOException {
return ZonedDateTime.parse(in.nextString());
}
})
.enableComplexMapKeySerialization()
.create();

关于java - 将 ZonedDateTime 类型转换为 Gson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36408192/

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