草庐IT

java - RestTemplate 无法填充对象

coder 2024-04-05 原文

我有以下代码来发送请求和接收响应,似乎一切都已配置但代码正在返回 NullPointerException

我不确定缺少什么。我打印出具有正确 URL 地址的 readyURL 变量。 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;
  }
}

异常

java.lang.NullPointerException
    at com.myproject.tickets.service.TicketSeviceImpl.seatWave(TicketSeviceImpl.java:245)
    at com.myproject.tickets.service.TicketSeviceImpl.findTicket(TicketSeviceImpl.java:45)
    at com.myproject.web.TicketController.findTicket(TicketController.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:112)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

示例结果

<?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=&amp;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&amp;affid=&amp;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=&amp;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&amp;affid=&amp;appid=2037810</SwSellURL>
      </Event>
      <Event>
         <Id>948273</Id>
         <Date>2015-09-27T15:00:00</Date>
         <EventGroupName>Matilda The Musical</EventGroupName>
         <VenueName>Cambridge Theatre</VenueName>

更新

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

if(eventResponse == null)
            {
                System.err.println("it is null");
            }else{

            System.err.println("message:>>"+eventResponse.getStatus().getMessage());
            }

最佳答案

我通过一些映射更改使您的代码正常工作:

@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
}

然后我使用 Wiremock 测试了它:

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=&amp;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&amp;affid=&amp;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=&amp;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&amp;affid=&amp;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 和集合的阅读:http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html (注意:不是我的博客)


使用的版本:

  • spring-web: 4.0.5.RELEASE
  • jaxb-api: 2.2.1
  • 线模:1.57

关于java - RestTemplate 无法填充对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32806530/

有关java - RestTemplate 无法填充对象的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  4. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  5. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  8. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  9. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  10. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

随机推荐