2016-10-18 47 views
1

我正在致力於GoogleAppEngine以構建RESTful Web服務。 一切都很好,但我接受一個簡單的bean簡單的方法:GAE映射從請求到Bean的JSON對象

@ApiMethod(name = "echo") 
public String echo(Update message) { 
    log.info("The message is: " + message.toString()); 
    return "done"; 
} 

的要求是這樣的:

{"id":123, "first_name":"hello", "user_name": "world"} 

的bean是這樣的:

import com.google.gson.annotations.SerializedName; 
import com.google.appengine.repackaged.org.codehaus.jackson.annotate.JsonProperty; 
public class Update { 

    private Integer id; 

    private String firstName; 

    private String userName; 

    // getter/setter 
} 

但是,由於json的關鍵字(first_name)與java bean(firstName)不同,我在echo方法的唯一標識符處收回!

我已經試過了變量,但沒有之前添加@SerializedName("user_name")@JsonProperty("user_name") .. 我不能改變的JSON的請求,我不希望重命名bean的領域。

有什麼建議嗎? Regards

回答

1

我找到了。

使用:

import com.google.appengine.repackaged.org.codehaus.jackson.annotate.JsonProperty; 

故障原因

我將其更改爲:

com.fasterxml.jackson.annotation.JsonProperty 

,現在,它的工作原理

this post give me the idea