0
我有我嘗試使用自定義讀者REST的服務,這是類:的Java Web服務JSON
@Stateless
@Path("person")
public class PersonFacadeREST extends AbstractFacade<Person>
implements javax.ws.rs.ext.MessageBodyReader<Person>
{
public boolean isReadable(Class<?> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType) {
System.out.println("isReadable????");
return Person.class == type;
}
public Person readFrom(Class<Person> type,
Type genericType,
Annotation[] annotations,
MediaType mediaType,
MultivaluedMap<String, String> httpHeaders,
InputStream entityStream) throws IOException, WebApplicationException {
/* This InputStream reads from the entityStream and constructs the object. */
System.out.println("Reading data!!!!!");
Person retObj = new Person();
System.out.println("Read data!!!!!!!!");
return retObj;
}
REST methods snipped...
我得到一個錯誤:java.lang.IllegalArgumentException: object is not an instance of declaring class
這裏是應用程序類:
@javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
addRestResourceClasses(resources);
return resources;
}
/**
* Do not modify addRestResourceClasses() method.
* It is automatically populated with
* all resources defined in the project.
* If required, comment out calling this method in getClasses().
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(net.mikeski.demo.service.rest.PersonFacadeREST.class);
}
}
我這樣做是因爲我有有沒有被正確解析的Map<String, POJO>
屬性的類。如果我刪除了MessageBodyReader的實現,則該服務將起作用。我試圖解析的對象是:
@Entity
@Table(name = "ent_person")
public class Person implements Serializable, Comparable<Person> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
/**
* Comment for <code>serialVersionUID</code>
*/
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;
的電話號碼與3字符串性質的POJO:
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;
public PhoneNumber() {
super();
}
/**
* @param countryCode
* @param areaCode
* @param subscriberNubmer
* @param extension
*/
public PhoneNumber(String countryCode, String areaCode, String subscriberNubmer,
String extension) {
super();
this.countryCode = countryCode;
this.areaCode = areaCode;
this.subscriberNubmer = subscriberNubmer;
this.extension = extension;
}
當化MessageBodyReader實現去除我發送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
}
}
}
如果我輸出上述JSON後,Web服務加載它,這裏是我得到的(注意phoneNumber字段是錯誤的):
{
"firstName":"John",
"id":1,
"idNum":"js3234",
"isMale":"n",
"lastName":"Smith",
"middleNames":["Stelling","Deering"],
"nickname":"JJ",
"phoneNumbers": {
"entry":[]
}
}
請你發佈完整的堆棧跟蹤? – javaguest
我放棄了這個,終於找到了一個可行的實現。請參閱下面的答案。 – mikeb