2015-11-02 45 views
0

我有這樣的Web服務方法。我將通過POST發送JSON映射到該方法,並且沒有正確設置地址字段。我知道JSON是有效的,因爲它直接將單元測試轉換爲JSON並將其讀回。測試通過(反序列化的對象有2個電話號碼),這是確切的字符串。傑克遜的Web服務 - 解析地圖<String,PhoneNumber>不起作用

@POST 
@Override 
@Consumes({"application/xml", "application/json"}) 
public void create(Person entity) { 
    System.out.println("Saving entity: " + entity); 
    super.create(entity); 
    System.out.println("Saved: " + entity); 
} 

當我發佈JSON它不會在對象上設置的電話號碼映射。下面是輸出:

信息:保存實體:個人{ID = NULL,名字=約翰,暱稱= JJ,姓氏=史密斯,middleNames = [Stelling,迪林],IDNUM = js3234,isMale = n,birthday = null,phoneNumbers = null}

Info:Saved:Person {id = 2,firstName = John,nickname = JJ,lastName = Smith,middleNames = [Stelling,Deering],idNum = js3234,isMale = N,生日= NULL,PHONENUMBERS = NULL}

這裏是JSON:

{ 
    "id": null, 
    "firstName": "John", 
    "nickname": "JJ", 
    "lastName": "Smith", 
    "middleNames": [ 
     "Stelling", 
     "Deering" 
    ], 
    "idNum": "js3234", 
    "isMale": "n", 
    "birthday": 778266673889, 
    "phoneNumbers": { 
     "Personal Mobile": { 
      "countryCode": "26", 
      "areaCode": "200", 
      "subscriberNubmer": "4069942", 
      "extension": null 
     }, 
     "Home": { 
      "countryCode": "79", 
      "areaCode": "115", 
      "subscriberNubmer": "9518863", 
      "extension": null 
     } 
    } 
} 

的對象是這樣的:

@Entity 
@Table(name = "ent_person") 
public class Person implements Serializable, Comparable<Person> { 

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Long id; 

private static final long serialVersionUID = -4680156785318108346L; 

protected String firstName; 

protected String nickname; 

protected String lastName; 

@ElementCollection(fetch = FetchType.EAGER) 
protected List<String> middleNames; 

protected String idNum; 

protected char isMale; 

@Temporal(value = TemporalType.DATE) 
protected Date birthday; 

@ElementCollection(fetch=FetchType.EAGER) 
@MapKeyColumn(name = "name") 
@Column(name = "value") 
protected Map<String, PhoneNumber> phoneNumbers; 
--- Snipped getters, setters, to string and constructors -- 

這是PhoneNumber對象

public class PhoneNumber implements Serializable { 

private static final long serialVersionUID = -423634682785318106L; 

public static transient final String HOME = "Home"; 

public static final String PERSONAL_MOBILE = "Personal Mobile"; 

public static final String OFFICE = "Office"; 

public static final String WORK_MOBILE = "Work Mobile"; 

public static final String FAX = "Fax"; 

public static final String PAGER = "Pager"; 

public static final String TOLL_FREE = "Toll Free"; 

public static final String OTHER = "Other"; 

String countryCode; 

String areaCode; 

String subscriberNubmer; 

String extension; 

回答

0

好了,我終於找到了答案。根據this table

我不得不添加一個自定義MessageBodyReader讀取字符串的地圖不會自動反序列化。由於我已經有一個工作單元測試來讀取字符串,所以添加正確的類和註釋以及之後的所有工作都很簡單:

@Consumes("application/json") 
@Provider 
public class PersonReader 
implements MessageBodyReader<Person> { 
    ObjectMapper mapper; 

    public PersonReader(){ 
     mapper = new ObjectMapper(); 
    } 

    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { 
     return type == Person.class; 
    } 

    public Person readFrom(Class<Person> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { 
     Person p = mapper.readValue(entityStream, Person.class); 
     return p; 
    } 
}