2

単純な ContentInput クラスを処理する単純なジャージ リソース UserContentManager があります。両方のクラスを以下に示します。postHello メソッドは us​​ing で呼び出されると正常に動作しますcurl -X POST -H "Content-Type: application/json" -d '{"id":1,"type":"a"}' localhost:50000/news/rest/helloが、putHello メソッドは with で呼び出されると失敗しますcurl -X PUT -H "Content-Type: application/json" -d '[{"id":1}]' localhost:50000/news/rest/hello

MOXyJsonProvider:598 (下の太字の行) で失敗しています。これは、アンマーシャリングされると、コードが期待するArrayList<Property>のではなくアンマーシャリングされるためです。ArrayList<JAXBElement<Property>>つまり、Object value = jaxbElement.getValue() はキャストとはArrayList<Property>異なります。ArrayList<JAXBElement>

これは Moxy のバグですか、それとも何か間違っていますか? 配列を返すとき、getArray メソッドは正常に動作しています。ContentInput クラスで @XmlRootElement の有無にかかわらず試してみましたが、結果は同じです。

        JAXBElement<?> jaxbElement = unmarshaller.unmarshal(jsonSource, domainClass);
        if(type.isAssignableFrom(JAXBElement.class)) {
            return jaxbElement;
        } else {
            Object value = jaxbElement.getValue();
            if(value instanceof ArrayList) {
                if(type.isArray()) {
                    ArrayList<JAXBElement> arrayList = (ArrayList<JAXBElement>) value;
                    int arrayListSize = arrayList.size();
                    Object array;
                    if(genericType instanceof GenericArrayType) {
                        array = Array.newInstance(JAXBElement.class, arrayListSize);
                        for(int x=0; x<arrayListSize; x++) {
                            Array.set(array, x, arrayList.get(x));
                        }
                    } else {
                        array = Array.newInstance(domainClass, arrayListSize);
                        for(int x=0; x<arrayListSize; x++) {

* Array.set(array, x, arrayList.get(x).getValue());*

                        }
                    }
                    return array;

@WebService
@Path("/hello")
public class UserContentManager {

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response postHello(ContentInput input) {
    input.setId(input.getId());
    input.setType("clip" + input.getType());
    ResponseBuilder builder = Response.ok();
        builder.entity(input);
    return builder.build();
}

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ContentInput[] putHello(ContentInput [] input) {
    return input;
}

@Path("/array")
@GET
@Produces(MediaType.APPLICATION_JSON)
public ContentInput[] getArray() {
    return new ContentInput[] {
            new ContentInput(),
            new ContentInput()
    };
}

}


public class ContentInput {
private int id;
private String type;

public ContentInput() {}

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getType() {
    return type;
}
public void setType(String type) {
    this.type = type;
}

}

4

1 に答える 1