所以,我有一个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的文档是拦截器部分的链接,但我不清楚这是什么问题。
提前感谢您的帮助!
答案 0 :(得分:2)
你的类路径中的jettison提供者?如果您使用的是maven,请添加此依赖项:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
将 ${resteasy.version}
替换为您正在使用的版本。
编辑:
您的参数或方法返回类型必须是一个泛型,其中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());
}
}