Java RestTemplate无法填充对象
我有以下代码来发送请求和接收响应,看起来一切都配置好了,但是代码返回Java RestTemplate无法填充对象,java,spring,rest,jaxb,spring-web,Java,Spring,Rest,Jaxb,Spring Web,我有以下代码来发送请求和接收响应,看起来一切都配置好了,但是代码返回NullPointerException 我不知道少了什么。我打印了readyURL变量,该变量具有正确的URL地址。stackTrace提供的信息不多 代码 try { final String APIKEY = "MYAPI"; final String URL = "http://api-sandbox.seatwave.com/v2/discovery/events?apikey="
NullPointerException
我不知道少了什么。我打印了readyURL变量,该变量具有正确的URL地址。stackTrace提供的信息不多
代码
try {
final String APIKEY = "MYAPI";
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()); //line 245
} catch (NullPointerException e) {
e.printStackTrace();
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class EventsResponse {
@XmlElement
private Status status;
@XmlElement(name = "Paging")
private Page page;
@XmlElement
private Events events;
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public Events getEvents() {
return events;
}
public void setEvents(Events events) {
this.events = events;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Status {
@XmlElement(name = "Version")
private double version;
@XmlElement(name = "TimeStampUtc")
private Date timeStampUtc;
@XmlElement(name = "Code")
private int code;
@XmlElement(name = "Message")
private String message;
@XmlElement(name = "Details")
private String details;
public double getVersion() {
return version;
}
public void setVersion(double version) {
this.version = version;
}
public Date getTimeStampUtc() {
return timeStampUtc;
}
public void setTimeStampUtc(Date timeStampUtc) {
this.timeStampUtc = timeStampUtc;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
}
@XmlRootElement(name="Page")
@XmlAccessorType(XmlAccessType.FIELD)
public class Page {
@XmlElement(name="PageNumber")
private int pageNumber;
@XmlElement(name="PageSize")
private int pageSize;
@XmlElement(name="PageResultCount")
private int pageResultCount;
@XmlElement(name="TotalResultCount")
private int totalResultCount;
@XmlElement(name="TotalPageCount")
private int totalPageCount;
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageResultCount() {
return pageResultCount;
}
public void setPageResultCount(int pageResultCount) {
this.pageResultCount = pageResultCount;
}
public int getTotalResultCount() {
return totalResultCount;
}
public void setTotalResultCount(int totalResultCount) {
this.totalResultCount = totalResultCount;
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Events {
@XmlElement
private List<Event> event;
public List<Event> getEvent() {
return event;
}
public void setEvent(List<Event> event) {
this.event = event;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Event {
@XmlElement(name = "Id")
private int id;
@XmlElement(name = "Date")
private Date date;
@XmlElement(name = "EventGroupName")
private String eventGroupName;
@XmlElement(name = "VenueName")
private String venueName;
@XmlElement(name = "Town")
private String town;
@XmlElement(name = "Country")
private String country;
@XmlElement(name = "TicketCount")
private int ticketCount;
@XmlElement(name = "Currency")
private String currency;
@XmlElement(name = "MinPrice")
private double minPrice;
@XmlElement(name = "SwURL")
private String swUrl;
@XmlElement(name = "EventGroupImageURL")
private String eventGroupImageUrl;
@XmlElement(name = "LayoutId")
private int layoutId;
@XmlElement(name = "EventGroupId")
private int eventGroupId;
@XmlElement(name = "VenueId")
private int venueId;
@XmlElement(name = "SwSellURL")
private String swSellUrl;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getEventGroupName() {
return eventGroupName;
}
public void setEventGroupName(String eventGroupName) {
this.eventGroupName = eventGroupName;
}
public String getVenueName() {
return venueName;
}
public void setVenueName(String venueName) {
this.venueName = venueName;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getTicketCount() {
return ticketCount;
}
public void setTicketCount(int ticketCount) {
this.ticketCount = ticketCount;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public double getMinPrice() {
return minPrice;
}
public void setMinPrice(double minPrice) {
this.minPrice = minPrice;
}
public String getSwUrl() {
return swUrl;
}
public void setSwUrl(String swUrl) {
this.swUrl = swUrl;
}
public String getEventGroupImageUrl() {
return eventGroupImageUrl;
}
public void setEventGroupImageUrl(String eventGroupImageUrl) {
this.eventGroupImageUrl = eventGroupImageUrl;
}
public int getLayoutId() {
return layoutId;
}
public void setLayoutId(int layoutId) {
this.layoutId = layoutId;
}
public int getEventGroupId() {
return eventGroupId;
}
public void setEventGroupId(int eventGroupId) {
this.eventGroupId = eventGroupId;
}
public int getVenueId() {
return venueId;
}
public void setVenueId(int venueId) {
this.venueId = venueId;
}
public String getSwSellUrl() {
return swSellUrl;
}
public void setSwSellUrl(String swSellUrl) {
this.swSellUrl = swSellUrl;
}
}
样本结果
<?xml version="1.0" encoding="UTF-8"?>
<EventsResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Status>
<Version>2.0</Version>
<TimeStampUtc>2015-09-27T08:44:24</TimeStampUtc>
<Code>0</Code>
<Message>Success</Message>
<Details />
</Status>
<Paging>
<PageNumber>1</PageNumber>
<PageSize>50</PageSize>
<PageResultCount>50</PageResultCount>
<TotalResultCount>7889</TotalResultCount>
<TotalPageCount>158</TotalPageCount>
</Paging>
<Events>
<Event>
<Id>948040</Id>
<Date>2015-09-27T14:30:00</Date>
<EventGroupName>The Lion King - London</EventGroupName>
<VenueName>Lyceum Theatre London</VenueName>
<Town>London</Town>
<Country>UK</Country>
<TicketCount>183</TicketCount>
<Currency>GBP</Currency>
<MinPrice>29.75</MinPrice>
<SwURL>http://www.seatwave.com/the-lion-king-london-tickets/lyceum-theatre--tickets/27-september-2015/perf/948040?affid=&appid=203710</SwURL>
<EventGroupImageURL>http://z.stwv.im/filestore/season/image/the-lion-king_000277_1_mainpicture.jpg</EventGroupImageURL>
<LayoutId>232</LayoutId>
<EventGroupId>277</EventGroupId>
<VenueId>232</VenueId>
<SwSellURL>http://www.seatwave.com/sellticketdetails?performanceId=948040&affid=&appid=2037810</SwSellURL>
</Event>
<Event>
<Id>987509</Id>
<Date>2015-09-27T15:00:00</Date>
<EventGroupName>American Idiot</EventGroupName>
<VenueName>Arts Theatre London</VenueName>
<Town>London</Town>
<Country>UK</Country>
<TicketCount>28</TicketCount>
<Currency>GBP</Currency>
<MinPrice>35.7</MinPrice>
<SwURL>http://www.seatwave.com/american-idiot-tickets/arts-theatre-tickets/27-september-2015/perf/987509?affid=&appid=2037810</SwURL>
<EventGroupImageURL>http://z.stwv.im/filestore/season/image/americanidiot_32152_1_1_20111209091615.jpg</EventGroupImageURL>
<LayoutId>4576</LayoutId>
<EventGroupId>32152</EventGroupId>
<VenueId>4207</VenueId>
<SwSellURL>http://www.seatwave.com/sellticketdetails?performanceId=987509&affid=&appid=2037810</SwSellURL>
</Event>
<Event>
<Id>948273</Id>
<Date>2015-09-27T15:00:00</Date>
<EventGroupName>Matilda The Musical</EventGroupName>
<VenueName>Cambridge Theatre</VenueName>
更新
try {
final String APIKEY = "MYAPI";
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()); //line 245
} catch (NullPointerException e) {
e.printStackTrace();
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class EventsResponse {
@XmlElement
private Status status;
@XmlElement(name = "Paging")
private Page page;
@XmlElement
private Events events;
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public Events getEvents() {
return events;
}
public void setEvents(Events events) {
this.events = events;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Status {
@XmlElement(name = "Version")
private double version;
@XmlElement(name = "TimeStampUtc")
private Date timeStampUtc;
@XmlElement(name = "Code")
private int code;
@XmlElement(name = "Message")
private String message;
@XmlElement(name = "Details")
private String details;
public double getVersion() {
return version;
}
public void setVersion(double version) {
this.version = version;
}
public Date getTimeStampUtc() {
return timeStampUtc;
}
public void setTimeStampUtc(Date timeStampUtc) {
this.timeStampUtc = timeStampUtc;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDetails() {
return details;
}
public void setDetails(String details) {
this.details = details;
}
}
@XmlRootElement(name="Page")
@XmlAccessorType(XmlAccessType.FIELD)
public class Page {
@XmlElement(name="PageNumber")
private int pageNumber;
@XmlElement(name="PageSize")
private int pageSize;
@XmlElement(name="PageResultCount")
private int pageResultCount;
@XmlElement(name="TotalResultCount")
private int totalResultCount;
@XmlElement(name="TotalPageCount")
private int totalPageCount;
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageResultCount() {
return pageResultCount;
}
public void setPageResultCount(int pageResultCount) {
this.pageResultCount = pageResultCount;
}
public int getTotalResultCount() {
return totalResultCount;
}
public void setTotalResultCount(int totalResultCount) {
this.totalResultCount = totalResultCount;
}
public int getTotalPageCount() {
return totalPageCount;
}
public void setTotalPageCount(int totalPageCount) {
this.totalPageCount = totalPageCount;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Events {
@XmlElement
private List<Event> event;
public List<Event> getEvent() {
return event;
}
public void setEvent(List<Event> event) {
this.event = event;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Event {
@XmlElement(name = "Id")
private int id;
@XmlElement(name = "Date")
private Date date;
@XmlElement(name = "EventGroupName")
private String eventGroupName;
@XmlElement(name = "VenueName")
private String venueName;
@XmlElement(name = "Town")
private String town;
@XmlElement(name = "Country")
private String country;
@XmlElement(name = "TicketCount")
private int ticketCount;
@XmlElement(name = "Currency")
private String currency;
@XmlElement(name = "MinPrice")
private double minPrice;
@XmlElement(name = "SwURL")
private String swUrl;
@XmlElement(name = "EventGroupImageURL")
private String eventGroupImageUrl;
@XmlElement(name = "LayoutId")
private int layoutId;
@XmlElement(name = "EventGroupId")
private int eventGroupId;
@XmlElement(name = "VenueId")
private int venueId;
@XmlElement(name = "SwSellURL")
private String swSellUrl;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getEventGroupName() {
return eventGroupName;
}
public void setEventGroupName(String eventGroupName) {
this.eventGroupName = eventGroupName;
}
public String getVenueName() {
return venueName;
}
public void setVenueName(String venueName) {
this.venueName = venueName;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getTicketCount() {
return ticketCount;
}
public void setTicketCount(int ticketCount) {
this.ticketCount = ticketCount;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
public double getMinPrice() {
return minPrice;
}
public void setMinPrice(double minPrice) {
this.minPrice = minPrice;
}
public String getSwUrl() {
return swUrl;
}
public void setSwUrl(String swUrl) {
this.swUrl = swUrl;
}
public String getEventGroupImageUrl() {
return eventGroupImageUrl;
}
public void setEventGroupImageUrl(String eventGroupImageUrl) {
this.eventGroupImageUrl = eventGroupImageUrl;
}
public int getLayoutId() {
return layoutId;
}
public void setLayoutId(int layoutId) {
this.layoutId = layoutId;
}
public int getEventGroupId() {
return eventGroupId;
}
public void setEventGroupId(int eventGroupId) {
this.eventGroupId = eventGroupId;
}
public int getVenueId() {
return venueId;
}
public void setVenueId(int venueId) {
this.venueId = venueId;
}
public String getSwSellUrl() {
return swSellUrl;
}
public void setSwSellUrl(String swSellUrl) {
this.swSellUrl = swSellUrl;
}
}
我在restTemplate.getForObject行之后添加了这段代码,但控制台上不会显示任何内容
if(eventResponse == null)
{
System.err.println("it is null");
}else{
System.err.println("message:>>"+eventResponse.getStatus().getMessage());
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公开课活动响应{
@XmlElementWrapper
@xmlement(name=“事件”)
私人列表活动;
}
并且可以删除Events类。我通过一些映射更改使您的代码正常工作:
@XmlRootElement(name = "EventsResponse") // added (name = "EventsResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class EventsResponse {
@XmlElement(name = "Status")
private Status status;
@XmlElement(name = "Paging")
private Page page;
@XmlElementWrapper(name="Events") // added
@XmlElement(name = "Event") // added (name = "Event")
private List<Event> events;
// getters, setter
}
@XmlRootElement // removed name =
@XmlAccessorType(XmlAccessType.FIELD)
public class Page {
@XmlElement(name = "PageNumber")
private int pageNumber;
@XmlElement(name = "PageSize")
private int pageSize;
@XmlElement(name = "PageResultCount")
private int pageResultCount;
@XmlElement(name = "TotalResultCount")
private int totalResultCount;
@XmlElement(name = "TotalPageCount")
private int totalPageCount;
// getters, setter
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Status {
@XmlElement(name = "Version")
private double version;
@XmlElement(name = "TimeStampUtc")
private Date timeStampUtc;
@XmlElement(name = "Code")
private int code;
@XmlElement(name = "Message")
private String message;
@XmlElement(name = "Details")
private String details;
// getters, setter
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Event {
@XmlElement(name = "Id")
private int id;
@XmlElement(name = "Date")
private Date date;
@XmlElement(name = "EventGroupName")
private String eventGroupName;
@XmlElement(name = "VenueName")
private String venueName;
@XmlElement(name = "Town")
private String town;
@XmlElement(name = "Country")
private String country;
@XmlElement(name = "TicketCount")
private int ticketCount;
@XmlElement(name = "Currency")
private String currency;
@XmlElement(name = "MinPrice")
private double minPrice;
@XmlElement(name = "SwURL")
private String swUrl;
@XmlElement(name = "EventGroupImageURL")
private String eventGroupImageUrl;
@XmlElement(name = "LayoutId")
private int layoutId;
@XmlElement(name = "EventGroupId")
private int eventGroupId;
@XmlElement(name = "VenueId")
private int venueId;
@XmlElement(name = "SwSellURL")
private String swSellUrl;
// getters, setter
}
输出:
seatwave>>>2
EventID: 948040
EventID: 987509
一些关于JAXB和集合的阅读:(注意:不是我的博客)
使用的版本:
- SpringWeb:4.0.5.0版本
- jaxb api:2.2.1
- wiremock:1.57
俄罗斯玩偶样式
生成xsd${home}/Desktop/schema.xsd
main
方法。在本例中,您应该得到50条记录作为大小。请注意,即使将name
变量更改为返回空集的值,也不会有NullPointerExeption
- 如果您使用的是maven,那么有一些漂亮的工具可以自动将jaxb类生成到
生成的源文件
文件夹中。我推荐
- 请注意,如果event属性为null,则
方法将返回一个新列表Events#getEvent()
- xml文件对于定制jaxb的生成方式非常方便,如果您使用它,我打赌您可以找到如何以更直观的方式与API类交互的方法。例如,在主类中,可以通过调用
而不是eventResponse.getEvents().size()
来获取大小eventResponse.getEvents().getEvent().size()
堆栈跟踪在哪里?你试过调试你的代码吗?您看到响应中有任何输出吗?@SMA堆栈跟踪包含在内。我还在努力。我检查了生成的URL是否正确并返回了正确的结果。哪一行是
t com.myproject.tickets.service.TicketSeviceImpl.seatWave(TicketSeviceImpl.java:245)
指的是什么?为什么人们在说出原因之前要单击“关闭”?你们应该告诉我你们的理由,这样我才能避免我做错了什么。所以,eventResponse.getEvents().getEvent().size()
抛出一个NPE。这意味着eventResponse为null,或者eventResponse.getEvents()为null,或者eventResponse.getEvents().getEvent()为null。使用您的调试器找出答案。谢谢,我做了同样的事情。我甚至更改了这两个的注释,但仍然引发了相同的异常@xmlement(name=“Events”)和@xmlement(name=“Event”)。我尝试过,但仍然有相同的问题。我会尝试wiremock。我添加了一些注释(添加/删除),我还完成了截断的xml(发布的一个是无效的)。因此,如果您不能让它与您的服务器一起工作,您应该会从restemplate
中获得一些异常,您知道WireMock的任何替代方案吗?替代方案做什么,我没有访问您的API服务器的权限,所以我模拟了它,您必须使用真实的东西(urlhttp://api-sandbox.seatwave.com/v2/discovery/events...
)。我确实上传了一个小项目,您可以按照自己的方式对其进行破解(响应位于src/main/resources/response.xml
)。
package so32806530;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.springframework.web.client.RestTemplate;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
public class App {
private static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<EventsResponse xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
" <Status>\n" +
" <Version>2.0</Version>\n" +
" <TimeStampUtc>2015-09-27T08:44:24</TimeStampUtc>\n" +
" <Code>0</Code>\n" +
" <Message>Success</Message>\n" +
" <Details />\n" +
" </Status>\n" +
" <Paging>\n" +
" <PageNumber>1</PageNumber>\n" +
" <PageSize>50</PageSize>\n" +
" <PageResultCount>50</PageResultCount>\n" +
" <TotalResultCount>7889</TotalResultCount>\n" +
" <TotalPageCount>158</TotalPageCount>\n" +
" </Paging>\n" +
" <Events>\n" +
" <Event>\n" +
" <Id>948040</Id>\n" +
" <Date>2015-09-27T14:30:00</Date>\n" +
" <EventGroupName>The Lion King - London</EventGroupName>\n" +
" <VenueName>Lyceum Theatre London</VenueName>\n" +
" <Town>London</Town>\n" +
" <Country>UK</Country>\n" +
" <TicketCount>183</TicketCount>\n" +
" <Currency>GBP</Currency>\n" +
" <MinPrice>29.75</MinPrice>\n" +
" <SwURL>http://www.seatwave.com/the-lion-king-london-tickets/lyceum-theatre--tickets/27-september-2015/perf/948040?affid=&appid=203710</SwURL>\n" +
" <EventGroupImageURL>http://z.stwv.im/filestore/season/image/the-lion-king_000277_1_mainpicture.jpg</EventGroupImageURL>\n" +
" <LayoutId>232</LayoutId>\n" +
" <EventGroupId>277</EventGroupId>\n" +
" <VenueId>232</VenueId>\n" +
" <SwSellURL>http://www.seatwave.com/sellticketdetails?performanceId=948040&affid=&appid=2037810</SwSellURL>\n" +
" </Event>\n" +
" <Event>\n" +
" <Id>987509</Id>\n" +
" <Date>2015-09-27T15:00:00</Date>\n" +
" <EventGroupName>American Idiot</EventGroupName>\n" +
" <VenueName>Arts Theatre London</VenueName>\n" +
" <Town>London</Town>\n" +
" <Country>UK</Country>\n" +
" <TicketCount>28</TicketCount>\n" +
" <Currency>GBP</Currency>\n" +
" <MinPrice>35.7</MinPrice>\n" +
" <SwURL>http://www.seatwave.com/american-idiot-tickets/arts-theatre-tickets/27-september-2015/perf/987509?affid=&appid=2037810</SwURL>\n" +
" <EventGroupImageURL>http://z.stwv.im/filestore/season/image/americanidiot_32152_1_1_20111209091615.jpg</EventGroupImageURL>\n" +
" <LayoutId>4576</LayoutId>\n" +
" <EventGroupId>32152</EventGroupId>\n" +
" <VenueId>4207</VenueId>\n" +
" <SwSellURL>http://www.seatwave.com/sellticketdetails?performanceId=987509&affid=&appid=2037810</SwSellURL>\n" +
" </Event>\n" +
" </Events>\n" +
"</EventsResponse>";
public static void main(final String[] args) {
// ---------- starts fake API server -----
final WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().port(8089));
wireMockServer.stubFor(get(urlMatching("/v2/discovery/events.*")).willReturn(aResponse().withHeader("Content-type", "application/xml").withStatus(200).withBody(XML)));
wireMockServer.start();
// ---------------------------------------
try {
final String name = "foo";
final String APIKEY = "MYAPI";
final String URL = "http://localhost:8089/v2/discovery/events?apikey=" + APIKEY;
final String readyUrl = URL + "&what=" + name;
final RestTemplate restTemplate = new RestTemplate();
final EventsResponse eventResponse = restTemplate.getForObject(readyUrl, EventsResponse.class);
System.err.println("seatwave>>>" + eventResponse.getEvents().size());
for (final Event event : eventResponse.getEvents()) {
System.out.println("EventID: " + event.getId());
}
} catch (final Exception ex) {
ex.printStackTrace();
}
// ---------- stops fake API server ------
wireMockServer.stop();
// ---------------------------------------
}
}
seatwave>>>2
EventID: 948040
EventID: 987509
<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>
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();
}
}
@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;
//getters & setters
@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;
}
}
@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;
//getter & setters