I managed to get Spring RestTemplate to work with SeatWave API
Here is a working github example: https://github.com/cosbor11/seatwave-spring-client-example
Here are the steps:
Enter the following GET URL into the browser:
http://api-sandbox.seatwave.com/v2/discovery/events?apikey=4739E4694D0E482A92C9D0B478D83934&what=music
- Take a look at the source code and copy the xml to the xsd generator .
- Generate xsd with
Russian Doll Style . - Paste the generated content into a new file located somewhere here:
${home}/Desktop/schema.xsd . - Delete the first line if it looks like this:
<?xml version="1.0" encoding="utf-16"?> Create a binding.xml file that looks like this:
<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" version="2.1"> <jaxb:globalBindings localScoping="toplevel"/> </jaxb:bindings>
- Create the src file:
mkdir src . Create Java Jaxb classes in this folder using XJC as follows:
xjc -b ~/Desktop/binding.xml -d src -p com.yourcompany ~/Desktop/schema.xsd
- Make sure you copy the generated classes and package structure to your build path.
Also, in your creation path, create a class using the main method, which uses your RestTemplate:
public static void main(String args[]) { try { String name = "music"; final String APIKEY = "4739E4694D0E482A92C9D0B478D83934"; //http://api-sandbox.seatwave.com/v2/discovery/events?apikey=4739E4694D0E482A92C9D0B478D83934&what=music final String URL = "http://api-sandbox.seatwave.com/v2/discovery/events?apikey=" + APIKEY; String readyUrl = URL + "&what=" + name; RestTemplate restTemplate = new RestTemplate(); EventsResponse eventResponse = restTemplate.getForObject(readyUrl, EventsResponse.class); System.err.println("seatwave>>>" + eventResponse.getEvents().getEvent().size()); } catch (NullPointerException e) { e.printStackTrace(); } }
Compile this class and run the main method. In this example, you should get 50 records as size. Note that even if you change the variable name to something that returns an empty set, there will be no NullPointerExeption .
Here's what the created JAXB classes look like:
EventResponse.java:
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "status", "paging", "events" }) @XmlRootElement(name = "EventsResponse") public class EventsResponse { @XmlElement(name = "Status", required = true) protected Status status; @XmlElement(name = "Paging", required = true) protected Paging paging; @XmlElement(name = "Events", required = true) protected Events events;
Events.java:
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "event" }) public class Events { @XmlElement(name = "Event", required = true) protected List<Event> event; public List<Event> getEvent() { if (event == null) { event = new ArrayList<Event>(); } return this.event; } }
Event.java:
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "id", "date", "eventGroupName", "venueName", "town", "country", "ticketCount", "currency", "minPrice", "swURL", "eventGroupImageURL", "layoutId", "eventGroupId", "venueId", "swSellURL" }) public class Event { @XmlElement(name = "Id") protected int id; @XmlElement(name = "Date", required = true) @XmlSchemaType(name = "dateTime") protected XMLGregorianCalendar date; @XmlElement(name = "EventGroupName", required = true) protected String eventGroupName; @XmlElement(name = "VenueName", required = true) protected String venueName; @XmlElement(name = "Town", required = true) protected String town; @XmlElement(name = "Country", required = true) protected String country; @XmlElement(name = "TicketCount") protected int ticketCount; @XmlElement(name = "Currency", required = true) protected String currency; @XmlElement(name = "MinPrice", required = true) protected BigDecimal minPrice; @XmlElement(name = "SwURL", required = true) protected String swURL; @XmlElement(name = "EventGroupImageURL", required = true) protected String eventGroupImageURL; @XmlElement(name = "LayoutId") protected int layoutId; @XmlElement(name = "EventGroupId") protected int eventGroupId; @XmlElement(name = "VenueId") protected int venueId; @XmlElement(name = "SwSellURL", required = true) protected String swSellURL;
Notes:
If you use maven, there are some great tools for automatically creating jaxb classes in your generated-sources folder. I recommend the cxf-xjc-plugin .
Note that the Events#getEvent() method returns a new list if the event property is null.
The binding.xml file is convenient for setting up the way you create your jaxb if you play with it. I bet you can figure out how to access the API classes in a more intuitive way. For example, in the main class, you can get the size by calling eventResponse.getEvents().size() instead of eventResponse.getEvents().getEvent().size() .