java – RestTemplate无法填充对象

前端之家收集整理的这篇文章主要介绍了java – RestTemplate无法填充对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有以下代码发送请求并收到响应,似乎所有内容都已配置但代码返回NullPointerException.

我不确定丢失了什么.我打印出了具有正确URL地址的readyURL变量. stackTrace提供的不多.

  1. try {
  2. final String APIKEY = "MYAPI";
  3. final String URL = "http://api-sandBox.seatwave.com/v2/discovery/events?apikey="
  4. + APIKEY;
  5. String readyUrl = URL + "&what=" + name;
  6. RestTemplate restTemplate = new RestTemplate();
  7. EventsResponse eventResponse = restTemplate.getForObject(readyUrl,EventsResponse.class);
  8. System.err.println("seatwave>>>"
  9. + eventResponse.getEvents().getEvent().size()); //line 245
  10. } catch (NullPointerException e) {
  11. e.printStackTrace();
  12. }
  13. @XmlRootElement
  14. @XmlAccessorType(XmlAccessType.FIELD)
  15. public class EventsResponse {
  16. @XmlElement
  17. private Status status;
  18. @XmlElement(name = "Paging")
  19. private Page page;
  20. @XmlElement
  21. private Events events;
  22. public Status getStatus() {
  23. return status;
  24. }
  25. public void setStatus(Status status) {
  26. this.status = status;
  27. }
  28. public Page getPage() {
  29. return page;
  30. }
  31. public void setPage(Page page) {
  32. this.page = page;
  33. }
  34. public Events getEvents() {
  35. return events;
  36. }
  37. public void setEvents(Events events) {
  38. this.events = events;
  39. }
  40. }
  41. @XmlRootElement
  42. @XmlAccessorType(XmlAccessType.FIELD)
  43. public class Status {
  44. @XmlElement(name = "Version")
  45. private double version;
  46. @XmlElement(name = "TimeStampUtc")
  47. private Date timeStampUtc;
  48. @XmlElement(name = "Code")
  49. private int code;
  50. @XmlElement(name = "Message")
  51. private String message;
  52. @XmlElement(name = "Details")
  53. private String details;
  54. public double getVersion() {
  55. return version;
  56. }
  57. public void setVersion(double version) {
  58. this.version = version;
  59. }
  60. public Date getTimeStampUtc() {
  61. return timeStampUtc;
  62. }
  63. public void setTimeStampUtc(Date timeStampUtc) {
  64. this.timeStampUtc = timeStampUtc;
  65. }
  66. public int getCode() {
  67. return code;
  68. }
  69. public void setCode(int code) {
  70. this.code = code;
  71. }
  72. public String getMessage() {
  73. return message;
  74. }
  75. public void setMessage(String message) {
  76. this.message = message;
  77. }
  78. public String getDetails() {
  79. return details;
  80. }
  81. public void setDetails(String details) {
  82. this.details = details;
  83. }
  84. }
  85. @XmlRootElement(name="Page")
  86. @XmlAccessorType(XmlAccessType.FIELD)
  87. public class Page {
  88. @XmlElement(name="PageNumber")
  89. private int pageNumber;
  90. @XmlElement(name="PageSize")
  91. private int pageSize;
  92. @XmlElement(name="PageResultCount")
  93. private int pageResultCount;
  94. @XmlElement(name="TotalResultCount")
  95. private int totalResultCount;
  96. @XmlElement(name="TotalPageCount")
  97. private int totalPageCount;
  98. public int getPageNumber() {
  99. return pageNumber;
  100. }
  101. public void setPageNumber(int pageNumber) {
  102. this.pageNumber = pageNumber;
  103. }
  104. public int getPageSize() {
  105. return pageSize;
  106. }
  107. public void setPageSize(int pageSize) {
  108. this.pageSize = pageSize;
  109. }
  110. public int getPageResultCount() {
  111. return pageResultCount;
  112. }
  113. public void setPageResultCount(int pageResultCount) {
  114. this.pageResultCount = pageResultCount;
  115. }
  116. public int getTotalResultCount() {
  117. return totalResultCount;
  118. }
  119. public void setTotalResultCount(int totalResultCount) {
  120. this.totalResultCount = totalResultCount;
  121. }
  122. public int getTotalPageCount() {
  123. return totalPageCount;
  124. }
  125. public void setTotalPageCount(int totalPageCount) {
  126. this.totalPageCount = totalPageCount;
  127. }
  128. }
  129. @XmlRootElement
  130. @XmlAccessorType(XmlAccessType.FIELD)
  131. public class Events {
  132. @XmlElement
  133. private List

例外

  1. java.lang.NullPointerException
  2. at com.myproject.tickets.service.TicketSeviceImpl.seatWave(TicketSeviceImpl.java:245)
  3. at com.myproject.tickets.service.TicketSeviceImpl.findTicket(TicketSeviceImpl.java:45)
  4. at com.myproject.web.TicketController.findTicket(TicketController.java:29)
  5. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  6. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  7. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  8. at java.lang.reflect.Method.invoke(Method.java:606)
  9. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
  10. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
  11. at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
  12. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
  13. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
  14. at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
  15. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
  16. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
  17. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
  18. at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
  19. at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
  20. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
  21. at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
  22. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
  23. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  24. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369)
  25. at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
  26. at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
  27. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
  28. at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
  29. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
  30. at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
  31. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
  32. at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
  33. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
  34. at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:112)
  35. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
  36. at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
  37. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
  38. at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
  39. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
  40. at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
  41. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
  42. at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
  43. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
  44. at org.springframework.security.web.authentication.logout.logoutFilter.doFilter(logoutFilter.java:105)
  45. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
  46. at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
  47. at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
  48. at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)
  49. at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
  50. at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
  51. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
  52. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  53. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  54. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  55. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  56. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
  57. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
  58. at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
  59. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  60. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
  61. at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
  62. at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
  63. at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
  64. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  65. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  66. at java.lang.Thread.run(Thread.java:744)

样本结果

  1. diotdiot-tickets/arts-theatre-tickets/27-september-2015/perf/987509?affid=&appid=2037810diot_32152_1_1_20111209091615.jpg

更新

我在restTemplate.getForObject行之后添加了此代码,但控制台上不会显示任何内容.

  1. if(eventResponse == null)
  2. {
  3. System.err.println("it is null");
  4. }else{
  5. System.err.println("message:>>"+eventResponse.getStatus().getMessage());
  6. }
最佳答案
我能够让Spring的RestTemplate与SeatWave’s API一起工作

这是github上的一个工作示例:https://github.com/cosbor11/seatwave-spring-client-example

以下是步骤:

>在浏览器中输入以下GET URL:

http://api-sandbox.seatwave.com/v2/discovery/events?apikey=4739E4694D0E482A92C9D0B478D83934&what=music
>查看源并将xml复制粘贴到xsd generator中.
>选择俄罗斯娃娃样式生成xsd.
>将生成内容粘贴到位于以下位置的新文件中:${home} /Desktop/schema.xsd.
>如果它看起来像这样删除第一行:<?xml version =“1.0”encoding =“utf-16”?>
>创建一个binding.xml文件,如下所示:

>创建一个src文件:mkdir src.
>使用XJC将Java Jaxb类生成到该文件夹​​中,如下所示:

xjc -b~ / Desktop / binding.xml -d src -p com.yourcompany~ / Desktop / schema.xsd
>确保将生成的类和包结构复制到构建路径中.
>此外,在构建路径中,使用使用RestTemplate的main方法创建一个类:

  1. public static void main(String args[])
  2. {
  3. try
  4. {
  5. String name = "music";
  6. final String APIKEY = "4739E4694D0E482A92C9D0B478D83934"; //http://api-sandBox.seatwave.com/v2/discovery/events?apikey=4739E4694D0E482A92C9D0B478D83934&what=music
  7. final String URL = "http://api-sandBox.seatwave.com/v2/discovery/events?apikey=" + APIKEY;
  8. String readyUrl = URL + "&what=" + name;
  9. RestTemplate restTemplate = new RestTemplate();
  10. EventsResponse eventResponse = restTemplate.getForObject(readyUrl,EventsResponse.class);
  11. System.err.println("seatwave>>>" + eventResponse.getEvents().getEvent().size());
  12. }
  13. catch (NullPointerException e)
  14. {
  15. e.printStackTrace();
  16. }
  17. }

>编译该类并运行main方法.在此示例中,您应该获得50条记录作为大小.请注意,即使将name变量更改为返回空集的内容,也不会有NullPointerExeption.

以下是生成的JAXB类的外观:

EventResponse.java:

  1. @XmlAccessorType(XmlAccessType.FIELD)
  2. @XmlType(name = "",propOrder = {
  3. "status","paging","events"
  4. })
  5. @XmlRootElement(name = "EventsResponse")
  6. public class EventsResponse {
  7. @XmlElement(name = "Status",required = true)
  8. protected Status status;
  9. @XmlElement(name = "Paging",required = true)
  10. protected Paging paging;
  11. @XmlElement(name = "Events",required = true)
  12. protected Events events;
  13. //getters & setters

Events.java:

  1. @XmlAccessorType(XmlAccessType.FIELD)
  2. @XmlType(name = "",propOrder = {
  3. "event"
  4. })
  5. public class Events {
  6. @XmlElement(name = "Event",required = true)
  7. protected List

Event.java:

  1. @XmlAccessorType(XmlAccessType.FIELD)
  2. @XmlType(name = "",propOrder = {
  3. "id","date","eventGroupName","venueName","town","country","ticketCount","currency","minPrice","swURL","eventGroupImageURL","layoutId","eventGroupId","venueId","swSellURL"
  4. })
  5. public class Event {
  6. @XmlElement(name = "Id")
  7. protected int id;
  8. @XmlElement(name = "Date",required = true)
  9. @XmlSchemaType(name = "dateTime")
  10. protected XMLGregorianCalendar date;
  11. @XmlElement(name = "EventGroupName",required = true)
  12. protected String eventGroupName;
  13. @XmlElement(name = "VenueName",required = true)
  14. protected String venueName;
  15. @XmlElement(name = "Town",required = true)
  16. protected String town;
  17. @XmlElement(name = "Country",required = true)
  18. protected String country;
  19. @XmlElement(name = "TicketCount")
  20. protected int ticketCount;
  21. @XmlElement(name = "Currency",required = true)
  22. protected String currency;
  23. @XmlElement(name = "MinPrice",required = true)
  24. protected BigDecimal minPrice;
  25. @XmlElement(name = "SwURL",required = true)
  26. protected String swURL;
  27. @XmlElement(name = "EventGroupImageURL",required = true)
  28. protected String eventGroupImageURL;
  29. @XmlElement(name = "LayoutId")
  30. protected int layoutId;
  31. @XmlElement(name = "EventGroupId")
  32. protected int eventGroupId;
  33. @XmlElement(name = "VenueId")
  34. protected int venueId;
  35. @XmlElement(name = "SwSellURL",required = true)
  36. protected String swSellURL;
  37. //getter & setters

Notes:

  • If you are using maven,there are some nifty tools to generate the jaxb classes into your generated-sources folder automatically. I recommend 07004.

  • Notice that Events#getEvent() method returns a new List if the event property is null.

  • The binding.xml file is handy for customizing the way your jaxb is generated,if you play around with it I bet you can figure out how to get the API classes to be interacted with in a more intuitive way. For example in you main class you could get the size by calling eventResponse.getEvents().size() instead of eventResponse.getEvents().getEvent().size().

猜你在找的Spring相关文章