javax.json.bind.JsonbException:反序列化的未解析类型:类java.lang.String

时间:2018-12-12 16:02:51

标签: java json spring json-deserialization jsonb-api

我的API中有一个DAO,例如:

@Id
private String idhousing;
private String owner;
private String name;
private String city;
private String type;
private int price;
private String description_detailed;
private String description_short;
private byte[] photo;
private int guests;
@JsonFormat(pattern="yyyy-MM-dd")
private LocalDate init_date;
@JsonFormat(pattern="yyyy-MM-dd")
private LocalDate end_date;
private String address;

在我的客户端应用程序中具有完全相同的DAO,我试图创建一个新的住房:

HousingObject house = new HousingObject(id_housing,owner,name,city,type,precio,
                    detailed_description,short_description,bytes,guests,localinitDate,localendDate,address);

            ClientConfig config = new ClientConfig();
            Client client = ClientBuilder.newClient();

            WebTarget webResource = client.target("http://localhost:10505").path("housings");
            Invocation.Builder invocationBuilder = webResource.request(MediaType.APPLICATION_JSON);
            Response respuesta = invocationBuilder.post(Entity.entity(house, MediaType.APPLICATION_JSON));

现在我要求获得所有住房,我的API是:

@RequestMapping(value = "/housings", method = RequestMethod.GET)
public @ResponseBody List<HousingObject> getAllHousingsbyEmail(@RequestParam("email") String email){

    for (HousingObject h : daoHsg.findByowner(email)) {
        System.out.println("Printing data...");
        System.out.println("The id_housing is " + h.getId_housing());
        System.out.println("init date is " + h.getInit_date());
        System.out.println("end date is " + h.getEnd_date());
        System.out.println("max guests are " + h.getGuests());
        System.out.println("the address is " + h.getAddress());
        System.out.println("-------------------------------------------");
    }

我从客户处调用aPI,并尝试打印我所有房屋的一些属性:

ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient();
WebTarget webResource = client.target("http://localhost:10505").
path("housings").queryParam("email",mail);
Invocation.Builder invocationBuilder  
= webResource.request(MediaType.APPLICATION_JSON);
Response respuesta = invocationBuilder.get(); 
int status = respuesta.getStatus();
HousingObject[] listHousings = respuesta.readEntity(HousingObject[].class);
for(HousingObject m : listHousings) {
    housingResult.add(m);
    System.out.println("-------------------------------------");
    System.out.println("printing data...");
    System.out.println("id_housing is " + m.getId_housing());
    System.out.println("init date is " + m.getInit_date());
    System.out.println("end date is " + m.getEnd_date());
    System.out.println("-------------------------------------");
    }

基本上,如果我从我的API打印列表:

Printing data...
The id_housing is 22ec2cc3-ae8e-4436-8fed-397c225b38e2
init date is 2018-06-23
end date is 2019-07-03
max guests are 5
the address is Collarte
-------------------------------------------

但是,如果我从客户端使用readEntity打印列表中每个对象的某些属性,如果列表中只有1个对象:

[2018-12-12T16:42:11.762+0100] [glassfish 5.0] [INFO] [] [] [tid: 
_ThreadID=29 _ThreadName=Thread-8] [timeMillis: 1544629331762] [levelValue: 
800] [[guests are 0]]

[2018-12-12T16:42:11.762+0100] [glassfish 5.0] [INFO] [] [] [tid: _ 
ThreadID=29 _ThreadName=Thread-8] [timeMillis: 1544629331762] [levelValue: 
800] [[price es 3]]

[2018-12-12T16:42:11.762+0100] [glassfish 5.0] [INFO] [] [] [tid: _ 
ThreadID=29 _ThreadName=Thread-8] [timeMillis: 1544629331762] [levelValue: 
800] [[type is es 3]]

[2018-12-12T16:42:11.762+0100] [glassfish 5.0] [INFO] [] [] [tid: 
_ThreadID=29 _ThreadName=Thread-8] [timeMillis: 1544629331762] [levelValue: 
800] [[owner is Carrillo@gmail.com]]

[2018-12-12T16:42:11.762+0100] [glassfish 5.0] [INFO] [] [] [tid: 
_ThreadID=29 _ThreadName=Thread-8] [timeMillis: 1544629331762] [levelValue: 
800] [[detailed_description is Good luck my friend]]

[2018-12-12T16:42:11.762+0100] [glassfish 5.0] [INFO] [] [] [tid: 
 _ThreadID=29 _ThreadName=Thread-8] [timeMillis: 1544629331762] [levelValue: 
 800] [[city is Madrid]]

[2018-12-12T16:42:11.762+0100] [glassfish 5.0] [INFO] [] [] [tid: 
_ThreadID=29 _ThreadName=Thread-8] [timeMillis: 1544629331762] [levelValue: 
800] [[short_description is wefwefw]]

[2018-12-12T16:42:11.762+0100] [glassfish 5.0] [INFO] [] [] [tid: 
_ThreadID=29 _ThreadName=Thread-8] [timeMillis: 1544629331762] [levelValue: 
800] [[address is null]]

[2018-12-12T16:42:11.762+0100] [glassfish 5.0] [INFO] [] [] [tid: _ 
ThreadID=29 _ThreadName=Thread-8] [timeMillis: 1544629331762] [levelValue: 
800] [[the id_housing is null]]

[2018-12-12T16:42:11.762+0100] [glassfish 5.0] [INFO] [] [] [tid: 
_ThreadID=29 _ThreadName=Thread-8] [timeMillis: 1544629331762] 
[levelValue:800] [[init date is null]]

[2018-12-12T16:42:11.762+0100] [glassfish 5.0] [INFO] [] [] [tid: 
_ThreadID=29 _ThreadName=Thread-8] [timeMillis: 1544629331762] [levelValue: 
800] [[end date is null]]

所以我不知道发生了什么。如果我使用Postman运行微服务,则列表打印将不会出现所有预期值的问题。如果我从api控制台打印列表,则可以从数据库中获得期望值。但是由于某种原因,当列表传送到我的客户端时,许多属性变为空,并且由于某些原因,属性来宾总是从其正确值更改为0(wtf)。

但是有趣的是,如果我的List有多个住房,那么我什至不能做readEntityt,因为抛出了异常:

Error deserializing object from entity stream. Caused by:
javax.json.bind.JsonbException: unresolved type for deserialization: 
class java.lang.String 

有任何提示吗?

0 个答案:

没有答案