簡體   English   中英

如何將Restlet請求的json類型輸入轉換為POJO?

[英]How to convert a json type input of a restlet request to a POJO?

我想編寫一個簡單的方法,該方法接收代表POJO的json並僅使用該POJO。

示例(接收和返回POJO):

@Put("json")
public Representation b(JacksonRepresentation<Device> deviceRepresentation)
        throws IOException {
    Device device = deviceRepresentation.getObject();
    // Use the device
    return new JacksonRepresentation<Device>(device);
}

上面的示例引發了一個異常: 屬性“ locationRef”的設置器定義沖突...

另一個選擇是使用JsonRepresentation,但我找不到將其轉換為POJO的方法:

@Put("json")
public Representation b(JsonRepresentation jsonRepresentation) {
    // How to convert the jsonRepresentation to a POJO???
    return new JsonRepresentation(new Device("2", 2));
}

傑克遜聽起來很不錯,因為它具有通用性,因此可以更好地保護類型-如果僅能起作用...

Ne需要使用任何表示對象。 在后台使用傑克遜 ,以下代碼效果很好:

@Put
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Device b(Device device) {
    // Do something with the POJO!!!
    return device;
}

它轉換輸入並轉換輸出。 這是一個如何工作的curl示例:

curl -i -X PUT -H 'Content-Type: application/json' -d '{"port":3,"ip":"3"}' http://localhost:8888/restlet/

結果:

HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Sun, 13 Oct 2013 02:03:48 GMT
Accept-Ranges: bytes
Server: Development/1.0
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Cache-Control: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Length: 19

{"ip":"3","port":3}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM