gpt4 book ai didi

java - Resteasy MessageBodyReader 错误信息

转载 作者:搜寻专家 更新时间:2023-11-01 03:27:49 24 4
gpt4 key购买 nike

所以,我有一个 REST 应用程序,看起来像这样:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/getpartners")
public HashMap<Integer, PartnerEntityDTO> getPartners() throws NamingException {
//body
}

我使用 ResteasyClient 框架实现了我的 REST 客户端,看起来像下面引用的 documentation

@GET
@Path("/getpartners")
@Produces(MediaType.APPLICATION_JSON)
HashMap<Integer, PartnerEntityDTO> getPartners();

关键是,我想从 REST 服务在客户端获取一个 HashMap,如果我使用浏览器,我可以毫无问题地获取它。但是如果我使用的是 swing 客户端,那么就会出现问题。我在我的 swing 客户端中使用这段代码来处理调用和响应。

    //Resteasy client
RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
PartnerServiceClientInterface partnerServiceClientInterface = ProxyFactory.create(PartnerServiceClientInterface.class, "http://localhost:8080/dilib/partners");
**//Sources.java:66**
String restResponse = partnerServiceClientInterface.getPartners().toString();
JsonParser jsonParser = new JsonParser();
JsonArray partnerDataInJson = jsonParser.parse(restResponse).getAsJsonArray();
int size = partnerDataInJson.size();
Gson gson = new Gson();
HashMap<Integer, PartnerEntityDTO> partnerDataFromServer = new HashMap<Integer, PartnerEntityDTO>();
for(int i = 0; i < size; i++) {
PartnerEntityDTO deserializedPartnerEntityDTO = gson.fromJson(partnerDataInJson.get(i), PartnerEntityDTO.class);
partnerDataFromServer.put(i, deserializedPartnerEntityDTO);
}

错误信息是这样的:

Exception in thread "AWT-EventQueue-0" org.jboss.resteasy.client.ClientResponseFailure: Unable to find a MessageBodyReader of content-type application/json and type java.util.HashMap<java.lang.Integer, com.sayusiando.java.dilib.dilibcore.server.entities.core.dto.PartnerEntityDTO>
at org.jboss.resteasy.client.core.BaseClientResponse.createResponseFailure(BaseClientResponse.java:522)
at org.jboss.resteasy.client.core.BaseClientResponse.createResponseFailure(BaseClientResponse.java:513)
at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:414)
at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:376)
at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:349)
at org.jboss.resteasy.client.core.extractors.BodyEntityExtractor.extractEntity(BodyEntityExtractor.java:54)
at org.jboss.resteasy.client.core.ClientInvoker.invoke(ClientInvoker.java:120)
at org.jboss.resteasy.client.core.ClientProxy.invoke(ClientProxy.java:88)
at $Proxy16.getPartners(Unknown Source)
at com.sayusiando.java.dilib.client.swing.modules.core.fields.sources.Sources.processPartnerDataFromRESTServer(Sources.java:66)
at com.sayusiando.java.dilib.client.swing.modules.core.fields.sources.Sources.initComponents(Sources.java:45)
at com.sayusiando.java.dilib.client.swing.modules.core.fields.sources.Sources.<init>(Sources.java:26)
at com.sayusiando.java.dilib.client.swing.modules.core.DilibCoreSwingClient.valueChanged(DilibCoreSwingClient.java:155)
at javax.swing.JTree.fireValueChanged(JTree.java:2820)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:3191)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1078)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:287)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:170)
at javax.swing.JTree.setSelectionPath(JTree.java:1598)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2311)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3520)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3459)
at java.awt.Component.processMouseEvent(Component.java:6285)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4235)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

我在谷歌上搜索了这条错误消息,Resteasy 的文档在拦截器部分有链接,但我不清楚问题出在哪里。

提前感谢您的帮助!

最佳答案

jettison 提供程序是否在您的类路径中?如果你使用的是 maven,请添加此依赖项:

    <dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>

${resteasy.version} 替换为您正在使用的版本。

编辑:

来自RestEASY documentation :

Your parameter or method return type must be a generic with a String as the key and the JAXB object's type.

工作测试用例:

import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.jboss.resteasy.plugins.server.tjws.TJWSEmbeddedJaxrsServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.naming.NamingException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.HashMap;
import java.util.Map;

import static junit.framework.Assert.assertEquals;

public class JsonMapTest {

@XmlRootElement
public static class PartnerEntityDTO {
@XmlElement String name;

public PartnerEntityDTO(){}
public PartnerEntityDTO(String name){ this.name = name; }
}

@Path("/")
public interface Service {
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/getpartners")
public Map<String, PartnerEntityDTO> getPartners() throws NamingException;
}

public static class ServiceImpl implements Service{

public Map<String, PartnerEntityDTO> getPartners() throws NamingException {
Map<String, PartnerEntityDTO> map = new HashMap<String, PartnerEntityDTO>();

map.put("1", new PartnerEntityDTO("one"));

return map;
}
}

static final TJWSEmbeddedJaxrsServer server = new TJWSEmbeddedJaxrsServer();

@BeforeClass
public static void start(){
server.setPort(8080);
server.start();
}

@AfterClass
public static void stop(){
server.stop();
}

@Test
public void test2() throws Exception {
server.getDeployment().getRegistry().addSingletonResource(new ServiceImpl());
String url = "http://localhost:8080";
Service service = ProxyFactory.create(Service.class, url);
Map<String, PartnerEntityDTO> partners = service.getPartners();

assertEquals(1, partners.size());
assertEquals("1", partners.keySet().iterator().next());
assertEquals("one", partners.values().iterator().next().name);

}

@Test
public void test() throws Exception {
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getRegistry().addSingletonResource(new ServiceImpl());

MockHttpRequest request = MockHttpRequest.get("/getpartners");
MockHttpResponse response = new MockHttpResponse();

dispatcher.invoke(request, response);

assertEquals("{\"1\":{\"partnerEntityDTO\":{\"name\":\"one\"}}}", response.getContentAsString());
}
}

关于java - Resteasy MessageBodyReader 错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8781769/

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