Resteasy MessageBodyReader 错误消息

发布于 2024-12-25 11:47:07 字数 6166 浏览 0 评论 0原文

所以,我有一个 REST 应用程序,如下所示:

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

我使用 ResteasyClient 框架实现了我的 REST 客户端,看起来像下面根据 文档

@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 的文档是 Interceptors 部分的 linket,但我不清楚问题是什么。

感谢您提前提供任何帮助!

So, I have a REST application and looks like this:

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

I implemented my REST client using ResteasyClient framework and looks like quoted below according to the documentation

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

The point is that, I would like to get an HashMap on client side from REST service and I get it without any issue if I'm usin a browser. But If I'm using swing client than something is wrong. I use this code in my swing client to handle the call and response.

    //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);
    }

The error message is this:

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)

I googled for this error message and the documentation of the Resteasy is linket at the Interceptors part, but it is not clear for me what is the problem.

Thanks for any help in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

救星 2025-01-01 11:47:07

抛弃提供者在你的类路径中吗?如果您使用的是 Maven,请添加此依赖项:

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

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

编辑:

来自 RestEASY文档

您的参数或方法返回类型必须是泛型,并以 String 作为键和 JAXB 对象的类型。

工作测试用例:

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());
    }
}

Is the jettison provider in your classpath? If you are using maven, add this dependency:

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

Replace ${resteasy.version} with the version you are using.

EDIT :

From the RestEASY documentation:

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

Working testcase:

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());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文