gpt4 book ai didi

java - 从 REST 请求获取响应时遇到问题 - Java NetBeans 和 Spring Beans

转载 作者:行者123 更新时间:2023-12-01 09:30:12 26 4
gpt4 key购买 nike

我正在尝试学习如何使用 Java 在 NetBeans 中使用 Spring 执行 REST 服务。

在我的应用程序中,我创建了一个 bean 并将依赖项放入我的 pom.xml 文件中。

当我发出请求时(我知道当我在浏览器中输入它并返回 JSON 时该请求有效),我收到以下错误:

--- exec-maven-plugin:1.2.1:exec (default-cli) @ AutoNGplusAPITest ---
Exception in thread "AWT-EventQueue-0" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface java.util.Map] and content type [text/html;charset=utf-8]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:773)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:758)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:535)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:489)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:431)
at com.blackstonesoftwaretechnologies.startPage.sendURIActionPerformed(startPage.java:467)
at com.blackstonesoftwaretechnologies.startPage.access$800(startPage.java:26)
at com.blackstonesoftwaretechnologies.startPage$9.actionPerformed(startPage.java:218)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 19.966s
Finished at: Wed Sep 14 11:15:07 EDT 2016
Final Memory: 6M/106M
<小时/>

这是我正在运行的 Java 类文件中传递 REST 调用的代码:

private void sendURIActionPerformed(java.awt.event.ActionEvent evt) {                                        
// TODO add your handling code here:
String link, api = null;
link = parseString(endpointAPI.getText());
shortResults.setText("URI under Test is: " + String.valueOf(link) + ". \n" + "Please wait ..." + "\n\n\n" + "Results: " + "\n");
//api = parseString(endpointAPI.getText());

Object[] row = {link, "", "200", ""};
DefaultTableModel model = (DefaultTableModel) parameterTable.getModel();
model.addRow(row);

RestTemplate restTemplate = new RestTemplate();
HttpHeaders requestHeaders = new HttpHeaders();

requestHeaders.add("Content-Type","application/json");

HttpEntity<String> request = new HttpEntity<>(requestHeaders);

String tokenaccess = "";
String apiUndertest1 = restTemplate.exchange(link, HttpMethod.GET, request, String.class).getBody();
Map result = restTemplate.exchange(link, HttpMethod.GET, request, Map.class).getBody();

shortResults.append(apiUndertest1);

}

我错过了什么?我假设在 pom.xml 文件中,我下载的 spring 依赖项将负责 JSON 转换。

我使用的示例网址是:https://github.comhttp://jsonplaceholder.typicode.com/posts/1

有人可以帮助我理解我在哪里犯了错误,并告诉我如何纠正它(缺少代码、代码结构等)。我真的很想正确地学习这一点。

谢谢

最佳答案

Spring Rest 模板不会自动将 JSON 映射到对象。您必须明确定义它。使用 Jackson(核心、数据绑定(bind)、注释)。将以下内容放入调度程序 servlet 中:

<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>

这将负责将 JSON 转换为 Class 对象,反之亦然。

理想情况下,这应该可以达到目的。

关于java - 从 REST 请求获取响应时遇到问题 - Java NetBeans 和 Spring Beans,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39495923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com