Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Web服务JSON_Java_Json - Fatal编程技术网

Java Web服务JSON

Java Web服务JSON,java,json,Java,Json,我有一个REST服务,我正在尝试使用自定义读取器,这是一个类: @Stateless @Path("person") public class PersonFacadeREST extends AbstractFacade<Person> implements javax.ws.rs.ext.MessageBodyReader<Person> { public boolean isReadable(Class<?> type,

我有一个REST服务,我正在尝试使用自定义读取器,这是一个类:

@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...
我这样做是因为我有一个类的
Map
属性没有被正确解析。如果我删除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
        }
    }
}
如果我在web服务加载上面的JSON后输出它,我得到的结果如下(注意phoneNumber字段是错误的):


我放弃了这样做,增加了这个类,解决了这个问题:

@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;
    }
}
@消耗(“应用程序/json”)
@提供者
公共类PersonReader
实现MessageBodyReader{
对象映射器映射器;
公众人物读取器(){
映射器=新的ObjectMapper();
}
公共布尔值可读取(类类型、类型genericType、注释[]注释、MediaType MediaType){
返回类型==Person.class;
}
public Person readFrom(类类型、类型genericType、注释[]注释、MediaType MediaType、多值Map HttpHeader、InputStream entityStream)引发IOException、WebApplicationException{
Person p=mapper.readValue(entityStream,Person.class);
返回p;
}
}

你能发布完整的堆栈跟踪吗?我放弃了这个,终于找到了一个可行的实现。见下面的答案。
{
    "firstName":"John",
    "id":1,
    "idNum":"js3234",
    "isMale":"n",
    "lastName":"Smith",
    "middleNames":["Stelling","Deering"],
    "nickname":"JJ",
    "phoneNumbers": {
        "entry":[]
    }
 }
@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;
    }
}