rakesh roshan rakesh roshan - 3 months ago 22
Java Question

javax.xml.bind.UnmarshalException ibator

I have no Idea what i am doing wrong

@Path("/orderService")
public class PlaceOrder {

@POST
@Path("/placeOrder")
@Consumes({ MediaType.APPLICATION_JSON })
public void placeOrder(PlaceOrderRequest placeOrderRequest,@Context HttpHeaders headers, @Context HttpServletRequest request,@Context HttpServletResponse response)
throws GwExceptions {
if(5>2){
boolean truechils=true;
}
}
}


and this is My placeOderReques class

@XmlRootElement(name = "placeorderequest")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"userName","numberOfItem","orderTimeslot",
"userAddress","addressLandmark","orderStatus","totalPrice"})
public class PlaceOrderRequest extends RestApiResponseParent{

@XmlElement(name = "userName", required = true)
@SerializedName("userName")
private String userName;

@XmlElement(name = "numberOfItem", required = true)
@SerializedName("numberOfItem")
private int numberOfItem;

@XmlElement(name = "orderTimeslot", required = true)
@SerializedName("orderTimeslot")
private Date orderTimeslot;

@XmlElement(name = "userAddress", required = true)
@SerializedName("userAddress")
private String userAddress;

@XmlElement(name = "addressLandmark", required = true)
@SerializedName("addressLandmark")
private String addressLandmark;

@XmlElement(name = "orderStatus", required = true)
@SerializedName("orderStatus")
private String orderStatus;

@XmlElement(name = "totalPrice", required = true)
@SerializedName("totalPrice")
private Double totalPrice;


public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public int getNumberOfItem() {
return numberOfItem;
}

public void setNumberOfItem(int numberOfItem) {
this.numberOfItem = numberOfItem;
}

public Date getOrderTimeslot() {
return orderTimeslot;
}

public void setOrderTimeslot(Date orderTimeslot) {
this.orderTimeslot = orderTimeslot;
}

public String getUserAddress() {
return userAddress;
}

public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}

public String getAddressLandmark() {
return addressLandmark;
}

public void setAddressLandmark(String addressLandmark) {
this.addressLandmark = addressLandmark;
}

public String getOrderStatus() {
return orderStatus;
}

public void setOrderStatus(String orderStatus) {
this.orderStatus = orderStatus;
}

public Double getTotalPrice() {
return totalPrice;
}

public void setTotalPrice(Double totalPrice) {
this.totalPrice = totalPrice;
}
}


*I am using rest api client to send the request * here is the error log




Caused by: com.sun.istack.SAXParseException2; columnNumber: 0;
unexpected element (uri:"", local:"userName").
Expected elements are <{}common_object>,
<{}placeorderequest>,
<{}response_parent>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:662)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:258)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:253)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:120)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1063)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:498)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:480)
at com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:75)
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:246)
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:180)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:415)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:386)
at org.apache.cxf.jaxrs.provider.json.JSONProvider.readFrom(JSONProvider.java:232)
... 43 more
Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"userName").
Expected elements are <{}common_object>,
<{}placeorderequest>,
<{}response_parent>


response which i am receiving is

JAXBException occurred : unexpected element (uri:"", local:"userName").
Expected elements are <{}common_object>,
<{}placeorderequest>
<{}response_parent>.
unexpected element (uri:"", local:"userName").
Expected elements are <{}common_object>,
<{}placeorderequest>,
<{}response_parent>.


Any help would be appreciated

k5_ k5_
Answer

An answer on an cxf-dev board suggests that you need to set the namespace for the @XmlRootElement

So: @XmlRootElement(name = "placeorderequest", namespace="")