Converting ZonedDateTime type to Gson

后端 未结 4 1897
梦谈多话
梦谈多话 2021-01-18 10:33

I have rest service that return arraylist of object,and I have implemented jersy restful client to execute it,but I have problem in converting ZonedDateTime type to json so

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-18 10:49

    I have fixed it,This is the code after updates

    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);
    
            Gson gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new JsonDeserializer() {
                @Override
                public ZonedDateTime deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
                    return ZonedDateTime.parse(json.getAsJsonPrimitive().getAsString());
                }
                }).create();
    
            Type listType =  new TypeToken>() {}.getType();
    
            List scorefactors = gson.fromJson(output,listType);
    

提交回复
热议问题