Developers Program

Java Sample: Use JAXB framework to make Trading XML API call over HTTP POST

Published: October 07 2010, 10:15:00 PMยทUpdated: August 17 2022, 9:50:30 PM

Products

Answer

Summary

This Java sample project illustrates the use of HttpURLConnection for posting Trading API Request XML to eBay trading API web service and JAXB framework for Java-XML mapping.

Two working sample applications: AddFixedPriceItemCall and GetSellerListCall are included in the attached project zip file.


SOFTWARE REQUIREMENTS
=====================
1. JAXWS2.1.5 or higher
- set the JWSDP_HOME system variable to point to your JAXWS2.1.5 installation directory.
2.JDK 1.6 or higher


INSTRUCTIONS
============

A. Binding Trading API Schema
1. Download the latest Trading API XSD file and save it to ${Trading_XML_Client_HOME}/XSD folder
2. Execute the jaxb-codeGen.xml script and generate java classes represent the Trading API Schema saved in step 1
> ant -f ${Trading_XML_Client_HOME}/jaxb-codeGen.xml

B. Build and run the samples

1. build and run the AddFixedPriceItemCall sample application
1.1 go to ${Trading_XML_Client_HOME}/samples/AddFixedPriceItemCall folder
1.2 edit the Configruation.xml file to specify your Sandbox user auth token
1.3 make neccessary modification to ${Trading_XML_Client_HOME}/samples/AddFixedPriceItemCall/src/com/ebay/sample/AddFixedPriceItemCall.java
1.4 execute the ant script to list an item to Sandbox environment
> ant -f build.xml

2. build and run the GetSellerListCall sample application
1.1 go to ${Trading_XML_Client_HOME}/samples/GetSellerListCall folder
1.2 edit the Configruation.xml file to specify your Sandbox user auth token
1.3 make neccessary modification to ${Trading_XML_Client_HOME}/samples/GetSellerListCall/src/com/ebay/sample/GetSellerListCall.java
1.4 execute the ant script to retrieve your active item list
> ant -f build.xml

Implmentation Detailes

1. Create a request type object, GetSellerListRequestType for example.

// create and initial an GetSellerListRequestType object
GetSellerListRequestType request = new GetSellerListRequestType();
XMLRequesterCredentialsType requestCredential = new XMLRequesterCredentialsType();
requestCredential.setEBayAuthToken(sample.USERTOKEN);
request.setRequesterCredentials(requestCredential);
java.util.Calendar nowCal = java.util.Calendar.getInstance();
java.util.Calendar fromCal = (java.util.Calendar) nowCal.clone();
XMLGregorianCalendar xmlgregorianTo = toApiTime(nowCal);
XMLGregorianCalendar xmlgregorianFrom = fromApiTime(fromCal,24);

request.setErrorLanguage("en_US");
request.setIncludeWatchCount(Boolean.TRUE);
request.setStartTimeFrom(xmlgregorianFrom);
request.setStartTimeTo(xmlgregorianTo);

2. Marshal JAXB object ( GetSellerListRequestType in this sample ) to XML request document
NOTE. For XML API request, you need to register your RequestCredentials( user token) object to request object ( AddFixedPriceItemRequestType, for example)

/*
* marshal GetSellerListRequestType object to XML data file
*
*/
private String marshalObject2Xml(GetSellerListRequestType requestobj) throws Exception {
String requestXmlFileName = "GetSellerListRequest.xml";
jc = JAXBContext.newInstance(JAXB_PACKAGE_NAME);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
ObjectFactory of = new ObjectFactory();

JAXBElement<GetSellerListRequestType> requestElement = of.createGetSellerListRequest(requestobj);
m.marshal(requestElement, new FileWriter(requestXmlFileName));
return requestXmlFileName;
}

3. Create HttpsURLConnection object, specify the required Trading API HTTP Headers and send the generated XML request document to the Trading API URL ( Sandbox : https://api.sandbox.ebay.com/ws/api.dll).

/*
* open SSL https connection and send the request xml data
*
*/
public InputStreamReader openConnectionFireRequest(String XmlFileName, String ApiCallName) throws Exception {
URL url = new URL(SERVERURL);
// open SSL https connection
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");

conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml");
// required headers
conn.setRequestProperty("X-EBAY-API-COMPATIBILITY-LEVEL", "1235");
conn.setRequestProperty("X-EBAY-API-CALL-NAME", ApiCallName);
conn.setRequestProperty("X-EBAY-API-SITEID", "0");

PrintWriter output = new PrintWriter(new OutputStreamWriter(conn.getOutputStream()));
Thread.sleep(1000);
String fileContent = convertFileContent2String(XmlFileName);

System.out.println(conn.getURL());
System.out.println(fileContent);
output.println(fileContent);
output.close();
conn.connect();
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
return isr;
}

4. Read the input stream returned in HttpsURLConnection.GetInputStream() to XML string and then unmarshal the XML document string to its corresponding Java Object ( GetSellerLisResponseType in our use case) for processing.

/*
* read input stream and unmarshal it into a Java content tree
*/
public Object unmarshalInputStream(InputStreamReader isr) {
Object returnedObj = null;
try {
int c = 0;
String addFixedPriceItemResponse = "GetSellerListResponse.xml";
OutputStream f = new FileOutputStream(addFixedPriceItemResponse);
while ((c = isr.read()) > -1) {
f.write(c);
}
f.close();
String responseXML = convertFileContent2String(addFixedPriceItemResponse);
System.out.println(responseXML);
InputStream input = new ByteArrayInputStream(responseXML.getBytes());
JAXBContext context = JAXBContext.newInstance(JAXB_PACKAGE_NAME);
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement<Object> poe = (JAXBElement) unmarshaller.unmarshal(input);
returnedObj = poe.getValue();

isr.close();
} catch (Exception e) {
//handle exception
}
return returnedObj;
}

Attachments

How well did this answer your question?