RestTemplateのレスポンスのマッピングについて
Q&A
Closed
RestTemplateのレスポンスのマッピン方法がわかりません
springbootのRestTemplateを使用してebayapiを呼び出し、結果をentity等にマッピングしてリストで保持したいのですがそのやり方がわかりません。
呼び出し箇所
ResponseEntity<FindItemsByCategoryResponse> response = restTemplate.getForEntity(findtemsUrl,
FindItemsByCategoryResponse.class);
FindItemsByCategoryResponse itemResponse = response.getBody();
findtemsUrlの中身
String findtemsUrl = "https://svcs.ebay.com/services/search/FindingService/v1"
+ "?OPERATION-NAME=findItemsByCategory"
+ "&SERVICE-VERSION=1.0.0"
+ "&SECURITY-APPNAME=**********"
+ "&RESPONSE-DATA-FORMAT=JSON"
+ "&REST-PAYLOAD"
+ "&categoryId=10181";
レスポンス
{"findItemsByCategoryResponse":[{"ack":["Success"],"version":["1.13.0"],"timestamp":["2023-09-12T03:48:58.196Z"],"searchResult":[{"@count":"80","item":[{"itemId":["311622236897"],"title":["CLARINETS-BANKRUPTCY SALE-NEW INTERMEDIATE CONCERT BAND CLARINET-W\/ YAMAHA PADS"],"globalId":["EBAY-US"],"subtitle":["FEATURED AT 2023 USA NAMM SHOW"],"primaryCategory":[{"categoryId":["47096"],"categoryName":["Clarinets"]}],"galleryURL":["https:\/\/i.ebayimg.com\/thumbs\/images\/g\/DKAAAOSwDiBZEN3m\/s-l140.jpg"],"viewItemURL":["https:\/\/www.ebay.com\/itm\/CLARINETS-BANKRUPTCY-SALE-NEW-INTERMEDIATE-CONCERT-BAND-CLARINET-W-YAMAHA-PADS-\/311622236897"],"autoPay":["true"],"postalCode":["386**"],"location":["Ripley,MS,USA"],"country":["US"],"shippingInfo":[{"shippingServiceCost":[{"@currencyId":"USD","__value__":"0.0"}],"shippingType":["Free"],"shipToLocations":["Worldwide"],"expeditedShipping":["false"],"oneDayShippingAvailable":["false"],"handlingTime":["1"]}],"sellingStatus":[{"currentPrice":[{"@currencyId":"USD","__value__":"132.99"}],"convertedCurrentPrice":[{"@currencyId":"USD","__value__":"132.99"}],"sellingState":["Active"],"timeLeft":["P16DT16H57M52S"]}],"listingInfo":[{"bestOfferEnabled":["false"],"buyItNowAvailable":["false"],"startTime":["2016-05-28T20:46:50.000Z"],"endTime":["2023-09-28T20:46:50.000Z"],"listingType":["FixedPrice"],"gift":["false"],"watchCount":["2112"]}],"returnsAccepted":["true"],"condition":[{"conditionId":["1000"],"conditionDisplayName":["New"]}],"isMultiVariationListing":["false"],"topRatedListing":["true"]},
以下itemId毎に同じ内容が続きます
FindItemsByCategoryResponse
@JsonIgnoreProperties(ignoreUnknown = true)
public class FindItemsResponse {
@JsonProperty("findItemsByCategoryResponse")
private List<FindItemsByCategoryResponse> findItemsByCategoryResponse;
// getters and setters
@JsonIgnoreProperties(ignoreUnknown = true)
public static class FindItemsByCategoryResponse {
@JsonProperty("searchResult")
private List<SearchResult> searchResult;
// getters and setters
@JsonIgnoreProperties(ignoreUnknown = true)
public static class SearchResult {
@JsonProperty("@count")
private String count;
@JsonProperty("item")
private List<Item> items;
// getters and setters
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Item {
@JsonProperty("itemId")
private List<String> itemId;
@JsonProperty("title")
private List<String> title;
@JsonProperty("globalId")
private List<String> globalId;
@JsonProperty("primaryCategory")
private List<PrimaryCategory> primaryCategory;
@JsonProperty("galleryURL")
private List<String> galleryURL;
@JsonProperty("viewItemURL")
private List<String> viewItemURL;
@JsonProperty("autoPay")
private List<String> autoPay;
@JsonProperty("postalCode")
private List<String> postalCode;
@JsonProperty("location")
private List<String> location;
@JsonProperty("country")
private List<String> country;
@JsonProperty("shippingInfo")
private List<ShippingInfo> shippingInfo;
@JsonProperty("sellingStatus")
private List<SellingStatus> sellingStatus;
@JsonProperty("listingInfo")
private List<ListingInfo> listingInfo;
@JsonProperty("returnsAccepted")
private List<String> returnsAccepted;
@JsonProperty("condition")
private List<Condition> condition;
@JsonProperty("isMultiVariationListing")
private List<String> isMultiVariationListing;
@JsonProperty("topRatedListing")
private List<String> topRatedListing;
// getters and setters
@JsonIgnoreProperties(ignoreUnknown = true)
public static class PrimaryCategory {
@JsonProperty("categoryId")
private List<String> categoryId;
@JsonProperty("categoryName")
private List<String> categoryName;
// getters and setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class ShippingInfo {
@JsonProperty("shippingServiceCost")
private List<ShippingServiceCost> shippingServiceCost;
@JsonProperty("shippingType")
private List<String> shippingType;
@JsonProperty("shipToLocations")
private List<String> shipToLocations;
@JsonProperty("expeditedShipping")
private List<String> expeditedShipping;
@JsonProperty("oneDayShippingAvailable")
private List<String> oneDayShippingAvailable;
@JsonProperty("handlingTime")
private List<String> handlingTime;
// getters and setters
@JsonIgnoreProperties(ignoreUnknown = true)
public static class ShippingServiceCost {
@JsonProperty("@currencyId")
private String currencyId;
@JsonProperty("__value__")
private String value;
// getters and setters
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class SellingStatus {
@JsonProperty("currentPrice")
private List<CurrentPrice> currentPrice;
@JsonProperty("convertedCurrentPrice")
private List<ConvertedCurrentPrice> convertedCurrentPrice;
@JsonProperty("sellingState")
private List<String> sellingState;
@JsonProperty("timeLeft")
private List<String> timeLeft;
// getters and setters
@JsonIgnoreProperties(ignoreUnknown = true)
public static class CurrentPrice {
@JsonProperty("@currencyId")
private String currencyId;
@JsonProperty("__value__")
private String value;
// getters and setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class ConvertedCurrentPrice {
@JsonProperty("@currencyId")
private String currencyId;
@JsonProperty("__value__")
private String value;
// getters and setters
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class ListingInfo {
@JsonProperty("bestOfferEnabled")
private List<String> bestOfferEnabled;
@JsonProperty("buyItNowAvailable")
private List<String> buyItNowAvailable;
@JsonProperty("startTime")
private List<String> startTime;
@JsonProperty("endTime")
private List<String> endTime;
@JsonProperty("listingType")
private List<String> listingType;
@JsonProperty("gift")
private List<String> gift;
@JsonProperty("watchCount")
private List<String> watchCount;
// getters and setters
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Condition {
@JsonProperty("conditionId")
private List<String> conditionId;
@JsonProperty("conditionDisplayName")
private List<String> conditionDisplayName;
// getters and setters
}
}
}
}
}
上記を実行して発生したエラーは以下です
org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.myapp.form.FindItemsResponse$FindItemsByCategoryResponse] and content type [text/plain;charset=UTF-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:124) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1132) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1115) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:865) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:764) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:405) ~[spring-web-6.0.11.jar:6.0.11]
at com.myapp.service.SellerResearchService.findingebaySellerInfo(SellerResearchService.java:102) ~[classes/:na]
at com.myapp.controller.SellerResearchController.sellerResearch(SellerResearchController.java:42) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1081) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.0.11.jar:6.0.11]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914) ~[spring-webmvc-6.0.11.jar:6.0.11]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:590) ~[tomcat-embed-core-10.1.11.jar:6.0]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) ~[spring-webmvc-6.0.11.jar:6.0.11]
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) ~[tomcat-embed-core-10.1.11.jar:6.0]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) ~[tomcat-embed-websocket-10.1.11.jar:10.1.11]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.11.jar:6.0.11]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.11.jar:6.0.11]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-6.0.11.jar:6.0.11]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.11.jar:6.0.11]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:166) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:894) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1740) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.11.jar:10.1.11]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
FindItemsByCategoryResponseの構成が悪いのか、そもそも他に原因があるのか全くわかりません。
どなたか教えていただけないでしょうか。
よろしくお願いいたします。
0 likes