spring 如何将XML响应从Roku Tv API转换为JSON?

yftpprvb  于 2023-11-16  发布在  Spring
关注(0)|答案(1)|浏览(130)

我试图构建一个使用Roku Tv ECP API命令的应用程序。我试图进行的特定调用获取所有已安装频道的列表,并以XML格式返回。每当我调用此API时,在提取响应时都会收到错误。
下面是我如何进行API调用:

  1. public class ConsumeXMLResponse {
  2. private RestTemplate restTemplate;
  3. private final Logger log = LoggerFactory.getLogger(ConsumeXMLResponse.class);
  4. public ConsumeXMLResponse(RestTemplate restTemplate) {
  5. this.restTemplate = restTemplate;
  6. }
  7. public ResponseEntity<Apps> get() {
  8. this.restTemplate = new RestTemplateBuilder().build();
  9. List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
  10. MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
  11. converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
  12. messageConverters.add(converter);
  13. this.restTemplate.setMessageConverters(messageConverters);
  14. log.info("This worked!");
  15. return this.restTemplate.getForEntity("http://10.0.0.229:8060/query/apps",
  16. Apps.class);
  17. }
  18. }

个字符
我试图从响应中重新创建的对象:

  1. @XmlRootElement(name = "apps")
  2. @XmlAccessorType(XmlAccessType.FIELD)
  3. public class Apps {
  4. @XmlElement(name = "app")
  5. private List<App> app;
  6. }
  1. @XmlAccessorType(XmlAccessType.FIELD)
  2. public class App {
  3. @XmlAttribute(name = "id")
  4. public String id;
  5. @XmlAttribute(name = "type")
  6. public String type;
  7. @XmlAttribute(name = "version")
  8. public String version;
  9. }

的字符串
下面是在postman中直接调用API时XML的示例:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <apps>
  3. <app id="tvinput.hdmi3" type="tvin" version="1.0.0">firestick</app>
  4. <app id="tvinput.hdmi1" type="tvin" version="1.0.0">Xbox</app>
  5. <app id="tvinput.hdmi2" type="tvin" version="1.0.0">Nintendo Switch</app>
  6. <app id="tvinput.dtv" type="tvin" version="1.0.0">Live TV</app>
  7. <app id="12" type="appl" version="5.1.120088002">Netflix</app>
  8. <app id="61322" type="appl" version="55.2.0">Max</app>
  9. <app id="13" type="appl" version="14.1.2023092022">Prime Video</app>
  10. <app id="2285" type="appl" version="6.75.2">Hulu</app>
  11. <app id="291097" type="appl" version="1.37.2023101000">Disney Plus</app>
  12. <app id="34376" type="appl" version="4.8.2023051800">ESPN</app>
  13. <app id="151908" type="appl" version="9.3.10">The Roku Channel</app>
  14. <app id="13842" type="appl" version="1.1.9">Vudu</app>
  15. <app id="46041" type="appl" version="8.96.12089">Sling TV - Live Sports, News, Shows + Freestream</app>
  16. <app id="45706" type="appl" version="1.0.60">Roku TV Intro</app>
  17. <app id="552944" type="appl" version="1.2.64">Roku Tips &amp; Tricks</app>
  18. <app id="562661" type="appl" version="1.2.28">Zoom TV for Kids</app>
  19. <app id="86398" type="appl" version="9.4.0">SYFY</app>
  20. <app id="593099" type="appl" version="4.11.12">Peacock TV</app>
  21. <app id="41468" type="appl" version="3.0.2">Tubi - Free Movies &amp; TV</app>
  22. <app id="31440" type="appl" version="8.8.202310302">Paramount Plus</app>
  23. <app id="dev" type="appl" version="1.0.1">MovieBoxPro</app>
  24. </apps>


我尝试调用返回XML响应的API,然后将其转换为Map到Apps类的Java对象。
这是一个错误:

  1. 2023-11-06T17:06:46.430-05:00 ERROR 76726 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.web.client.RestClientException: Error while extracting response for type [class com.TelevisionRemote.SmartTelevisionRemote.component.Apps] and content type [text/xml;charset="utf-8"]] with root cause
  2. com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
  3. at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 2]
  4. at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2418) ~[jackson-core-2.14.2.jar:2.14.2]
  5. at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:749) ~[jackson-core-2.14.2.jar:2.14.2]
  6. at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:673) ~[jackson-core-2.14.2.jar:2.14.2]
  7. at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2784) ~[jackson-core-2.14.2.jar:2.14.2]
  8. at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._nextTokenNotInObject(UTF8StreamJsonParser.java:907) ~[jackson-core-2.14.2.jar:2.14.2]
  9. at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:793) ~[jackson-core-2.14.2.jar:2.14.2]
  10. at com.fasterxml.jackson.databind.ObjectReader._initForReading(ObjectReader.java:357) ~[jackson-databind-2.14.2.jar:2.14.2]
  11. at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2095) ~[jackson-databind-2.14.2.jar:2.14.2]
  12. at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1481) ~[jackson-databind-2.14.2.jar:2.14.2]
  13. at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:395) ~[spring-web-6.0.6.jar:6.0.6]
  14. at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:354) ~[spring-web-6.0.6.jar:6.0.6]
  15. at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:103) ~[spring-web-6.0.6.jar:6.0.6]
  16. at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1132) ~[spring-web-6.0.6.jar:6.0.6]
  17. at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1115) ~[spring-web-6.0.6.jar:6.0.6]
  18. at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:865) ~[spring-web-6.0.6.jar:6.0.6]
  19. at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:764) ~[spring-web-6.0.6.jar:6.0.6]
  20. at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:405) ~[spring-web-6.0.6.jar:6.0.6]
  21. at com.TelevisionRemote.SmartTelevisionRemote.controller.ConsumeXMLResponse.get(ConsumeXMLResponse.java:38) ~[classes/:na]
  22. at com.TelevisionRemote.SmartTelevisionRemote.controller.RestXMLResponseController.getXMLOutput(RestXMLResponseController.java:28) ~[classes/:na]
  23. at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
  24. at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
  25. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:207) ~[spring-web-6.0.6.jar:6.0.6]
  26. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:152) ~[spring-web-6.0.6.jar:6.0.6]
  27. at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-6.0.6.jar:6.0.6]
  28. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) ~[spring-webmvc-6.0.6.jar:6.0.6]
  29. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-6.0.6.jar:6.0.6]
  30. at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.0.6.jar:6.0.6]
  31. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1081) ~[spring-webmvc-6.0.6.jar:6.0.6]
  32. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974) ~[spring-webmvc-6.0.6.jar:6.0.6]
  33. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.0.6.jar:6.0.6]
  34. at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) ~[spring-webmvc-6.0.6.jar:6.0.6]
  35. at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:705) ~[tomcat-embed-core-10.1.5.jar:6.0]
  36. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) ~[spring-webmvc-6.0.6.jar:6.0.6]
  37. at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:814) ~[tomcat-embed-core-10.1.5.jar:6.0]
  38. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  39. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  40. at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-10.1.5.jar:10.1.5]
  41. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  42. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  43. at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-6.0.6.jar:6.0.6]
  44. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.6.jar:6.0.6]
  45. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  46. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  47. at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-6.0.6.jar:6.0.6]
  48. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.6.jar:6.0.6]
  49. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  50. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  51. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-6.0.6.jar:6.0.6]
  52. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.6.jar:6.0.6]
  53. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  54. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  55. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  56. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  57. at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  58. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  59. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  60. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  61. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  62. at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:400) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  63. at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  64. at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  65. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1734) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  66. at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  67. at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  68. at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  69. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.5.jar:10.1.5]
  70. at java.base/java.lang.Thread.run(Thread.java:1623) ~[na:na]

iaqfqrcu

iaqfqrcu1#

事实证明,将@JacksonXmlElementWrapper添加到我的应用程序列表属性上的Apps类解决了这个问题。

  1. @JacksonXmlElementWrapper(useWrapping = false)
  2. private List<App> app;

字符串
我也不需要消息转换器。

  1. public Apps getXMLOutput() {
  2. ResponseEntity<Apps> responseEntity
  3. = this.restTemplate.getForEntity("http://10.0.0.229:8060/query/apps",
  4. Apps.class);
  5. Apps apps = responseEntity.getBody();
  6. log.info(apps.toString());
  7. return appsRepository.save(apps);
  8. }


Postman中的结果:

  1. {
  2. "id": 1,
  3. "app": [
  4. {
  5. "id": "tvinput.hdmi3",
  6. "type": "tvin",
  7. "version": "1.0.0"
  8. },
  9. {
  10. "id": "tvinput.hdmi1",
  11. "type": "tvin",
  12. "version": "1.0.0"
  13. },
  14. {
  15. "id": "tvinput.hdmi2",
  16. "type": "tvin",
  17. "version": "1.0.0"
  18. },
  19. {
  20. "id": "tvinput.dtv",
  21. "type": "tvin",
  22. "version": "1.0.0"
  23. },
  24. {
  25. "id": "12",
  26. "type": "appl",
  27. "version": "5.1.120088002"
  28. },
  29. {
  30. "id": "61322",
  31. "type": "appl",
  32. "version": "55.2.0"
  33. },
  34. {
  35. "id": "13",
  36. "type": "appl",
  37. "version": "14.1.2023092022"
  38. },
  39. {
  40. "id": "2285",
  41. "type": "appl",
  42. "version": "6.75.2"
  43. },
  44. {
  45. "id": "291097",
  46. "type": "appl",
  47. "version": "1.37.2023101000"
  48. },
  49. {
  50. "id": "34376",
  51. "type": "appl",
  52. "version": "4.8.2023051800"
  53. },
  54. {
  55. "id": "151908",
  56. "type": "appl",
  57. "version": "9.3.10"
  58. },
  59. {
  60. "id": "13842",
  61. "type": "appl",
  62. "version": "1.1.9"
  63. },
  64. {
  65. "id": "46041",
  66. "type": "appl",
  67. "version": "8.97.12290"
  68. },
  69. {
  70. "id": "45706",
  71. "type": "appl",
  72. "version": "1.0.60"
  73. },
  74. {
  75. "id": "552944",
  76. "type": "appl",
  77. "version": "1.2.64"
  78. },
  79. {
  80. "id": "562661",
  81. "type": "appl",
  82. "version": "1.2.28"
  83. },
  84. {
  85. "id": "86398",
  86. "type": "appl",
  87. "version": "9.4.0"
  88. },
  89. {
  90. "id": "593099",
  91. "type": "appl",
  92. "version": "4.11.12"
  93. },
  94. {
  95. "id": "41468",
  96. "type": "appl",
  97. "version": "3.0.2"
  98. },
  99. {
  100. "id": "31440",
  101. "type": "appl",
  102. "version": "8.8.202310302"
  103. },
  104. {
  105. "id": "dev",
  106. "type": "appl",
  107. "version": "1.0.1"
  108. }
  109. ]
  110. }

展开查看全部

相关问题