2015-11-03 70 views
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":[] 
    } 
} 
+0

请你发布完整的堆栈跟踪? – javaguest

+0

我放弃了这个,终于找到了一个可行的实现。请参阅下面的答案。 – mikeb

回答

0

我报废做事这种方式,并将此类,它解决了这个问题:

@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; 
    } 
}