Issue
I have a class ShipmentRequest which was initialized through JAXB.
In my main class to send a REST-API POST-Request I am filling an object of this class with the needed input and put it in a JSONArray
public JSONArray array = new JSONArray();
array.add(shipmentRequest);
And finally I am starting the POST:
HttpResponse<JsonNode> response = Unirest.post("https://someURL/services/rest/url")
.queryString("access_token","someTokenCode")
.body(array.get(0)).asJson();
I do not understand the error, because when I use myJSONArray.toJSONString() I get following output:
[{"Id":"123456","Reference":"Send","transport":{"transporterCode":"Deliverer","trackAndTrace":"someCode123456"}}]
What am I missing? My first character is a '[' ?
EDIT 1, Code of ShipmentRequest
//
// Diese Datei wurde mit der JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802 generiert
// Siehe <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Änderungen an dieser Datei gehen bei einer Neukompilierung des Quellschemas verloren.
// Generiert: 2017.10.18 um 11:02:47 AM CEST
//
package bol.shipment;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java-Klasse für anonymous complex type.
*
* <p>Das folgende Schemafragment gibt den erwarteten Content an, der in dieser Klasse enthalten ist.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="OrderItemId" type="{http://www.w3.org/2001/XMLSchema}long"/>
* <element name="ShipmentReference" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="Transport" minOccurs="0">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="TransporterCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="TrackAndTrace" type="{https://plazaapi.bol.com/services/xsd/v2/plazaapi.xsd}TrackAndTrace" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </element>
* <element name="ShippingLabelCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"orderItemId",
"shipmentReference",
"transport",
})
@XmlRootElement(name = "ShipmentRequest")
public class ShipmentRequest {
@XmlElement(name = "OrderItemId")
protected String orderItemId;
@XmlElement(name = "ShipmentReference", required = true)
protected String shipmentReference;
@XmlElement(name = "Transport")
protected ShipmentRequest.Transport transport;
/**
* Ruft den Wert der orderItemId-Eigenschaft ab.
*
*/
public String getOrderItemId() {
return orderItemId;
}
/**
* Legt den Wert der orderItemId-Eigenschaft fest.
*
*/
public void setOrderItemId(String value) {
this.orderItemId = value;
}
/**
* Ruft den Wert der shipmentReference-Eigenschaft ab.
*
* @return
* possible object is
* {@link String }
*
*/
public String getShipmentReference() {
return shipmentReference;
}
/**
* Legt den Wert der shipmentReference-Eigenschaft fest.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setShipmentReference(String value) {
this.shipmentReference = value;
}
/**
* Ruft den Wert der transport-Eigenschaft ab.
*
* @return
* possible object is
* {@link ShipmentRequest.Transport }
*
*/
public ShipmentRequest.Transport getTransport() {
return transport;
}
/**
* Legt den Wert der transport-Eigenschaft fest.
*
* @param value
* allowed object is
* {@link ShipmentRequest.Transport }
*
*/
public void setTransport(ShipmentRequest.Transport value) {
this.transport = value;
}
/**
* <p>Java-Klasse für anonymous complex type.
*
* <p>Das folgende Schemafragment gibt den erwarteten Content an, der in dieser Klasse enthalten ist.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="TransporterCode" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="TrackAndTrace" type="{https://plazaapi.bol.com/services/xsd/v2/plazaapi.xsd}TrackAndTrace" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"transporterCode",
"trackAndTrace"
})
public static class Transport {
@XmlElement(name = "TransporterCode")
protected String transporterCode;
@XmlElement(name = "TrackAndTrace")
protected String trackAndTrace;
/**
* Ruft den Wert der transporterCode-Eigenschaft ab.
*
* @return
* possible object is
* {@link String }
*
*/
public String getTransporterCode() {
return transporterCode;
}
/**
* Legt den Wert der transporterCode-Eigenschaft fest.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setTransporterCode(String value) {
this.transporterCode = value;
}
/**
* Ruft den Wert der trackAndTrace-Eigenschaft ab.
*
* @return
* possible object is
* {@link String }
*
*/
public String getTrackAndTrace() {
return trackAndTrace;
}
/**
* Legt den Wert der trackAndTrace-Eigenschaft fest.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setTrackAndTrace(String value) {
this.trackAndTrace = value;
}
}
}
EDIT 2: My Request should look like this!
<ShipmentRequest xmlns="https://plazaapi.bol.com/services/xsd/v2.1/plazaapi.xsd">
<OrderItemId>1234567890</OrderItemId>
<ShipmentReference>A321SR</ShipmentReference>
<Transport>
<TransporterCode>TNT</TransporterCode>
<TrackAndTrace>3SBOL0987654321</TrackAndTrace>
</Transport>
</ShipmentRequest>
EDIT 3: I tried filling a JSONObject with these:
private JSONArray shipmentRequestArray = new JSONArray();
private JSONArray transportArray = new JSONArray();
private JSONObject requestObject = new JSONObject();
transportArray.add(new JSONObject().append("TransporterCode", transporter_Code));
transportArray.add(new JSONObject().append("TrackAndTrace", track_And_Trace));
shipmentRequestArray.add(new JSONObject().append("OrderItemId", orderItemIdTEST));
shipmentRequestArray.add(new JSONObject().append("ShipmentReference", shipment_Reference));
shipmentRequestArray.add(transportArray);
requestObject.put("ShipmentRequest", shipmentRequestArray);
And when I tried this with:
.body(requestObject).asJson();
I get the new error: No serializer found for class org.codehaus.jettison.json.JSONObject and no properties discovered to create BeanSerializer
EDIT 4: My example response should look like this:
Status: 201 created
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:ProcessStatus xmlns:ns1="https://plazaapi.bol.com/services/xsd/v2/plazaapi.xsd">
<ns1:id>1</ns1:id>
<ns1:sellerId>1084486</ns1:sellerId>
<ns1:entityId>1234567890</ns1:entityId>
<ns1:eventType>CONFIRM_SHIPMENT</ns1:eventType>
<ns1:description>Confirm shipment for order item 1234567890.</ns1:description>
<ns1:status>PENDING</ns1:status>
<ns1:createTimestamp>2016-02-17T17:30:00.142+01:00</ns1:createTimestamp>
<ns1:Links>
<ns1:link ns1:method="GET" ns1:href="https://plazaapi.bol.com/services/rest/process-status/v2/1234567" ns1:rel="self"/>
</ns1:Links>
</ns1:ProcessStatus>
Solution
Error is about wrong type of object. First character should be '[', but it's not - I guess its '{'.
It is because you are executing array.get(0)
, which returns you an JSONObject, where JSONArray is required.
UPD:
I did some debugging with additional info you provided.
Problem was that Unirest.post
returns an object of RequestBodyEntity class. To convert it to json you need to get body of answer first via getBody()
.
Thats what worked for me:
ShipmentRequest shipmentRequest = new ShipmentRequest();
shipmentRequest.setOrderItemId("1234567890");
shipmentRequest.setShipmentReference("A321SR");
ShipmentRequest.Transport transport = new ShipmentRequest.Transport();
transport.setTrackAndTrace("3SBOL0987654321");
transport.setTransporterCode("TNT");
shipmentRequest.setTransport(transport);
String jsonRequest = new Gson().toJson(shipmentRequest);
RequestBodyEntity result = Unirest.post("https://plazaapi.bol.com/services/rest/shipments/v2")
.queryString(
"access_token",
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjaWQiOjMzMTgsInNlcSI6MSwidHlwIjoiYXBpIiwidnNuIjoxLCJuYmYiOjE1MTcyOTc2NjF9.hVdtwBfJAVfRVeBdf1XDAxe7Qpigwa0wmCvnF_Bicc8")
.body(jsonRequest);
System.out.println(new Gson().fromJson(result.getBody().toString(), ShipmentRequest.class));
You need to use result.getBody()
to return object that you want.
After this you can do
System.out.println(result.getBody().toString())
to print it or
new Gson().fromJson(result.getBody().toString(), ShipmentRequest.class)
to save it into object of ShipmentRequest type.
Answered By - Pavel Gordon