ฉันมีทรัพยากรเสื้อแบบธรรมดา UserContentManager ที่ประมวลผลคลาส ContentInput แบบธรรมดา ทั้งสองคลาสอยู่ด้านล่าง เมธอด postHello ทำงานได้ดีเมื่อถูกเรียกโดยใช้ curl -X POST -H "Content-Type: application/json" -d '{"id":1,"type":"a"}' localhost:50000/news/rest/hello
แต่เมธอด putHello นั้นล้มเหลวเมื่อถูกเรียกด้วย 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 ทำงานได้ดีเมื่อส่งคืนอาร์เรย์ ฉันได้ลองแล้วโดยมีและไม่มี @XmlRootElement ในคลาส ContentInput แต่ผลลัพธ์ก็เหมือนกัน
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(อาร์เรย์, 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;
}
}