RestEasy-无法找到MessageBodyReader? - java

我编写了一个简单的RestEasy客户端代理来执行iTunes搜索。看起来像这样:

@Path("/")
public interface AppleAppStoreLookupClient {

    /**
     * Attempts to lookup an apple app store item by its ID
     * 
     * @param id
     *            The item ID
     * @return The app details
     */
    @GET
    @Path("/lookup")
    @Produces(value = { "text/javascript" })
    public AppleAppDetailsResponse lookupByID(@QueryParam("id") String id);
}

我的JSON模型类也很简单。实际上,对于第一个调用,我想要的只是“resultCount”值,只是为了确保连接正常。

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class AppleAppDetailsResponse implements Serializable {

    private static final long serialVersionUID = 8881587082097337598L;

    @XmlElement
    private int resultCount = -1;

   ...getters and setters...

}

但是,当我运行一个简单的测试时,出现以下异常:

org.jboss.resteasy.client.ClientResponseFailure: Unable to find a MessageBodyReader of content-type text/javascript;charset="utf-8" and type class net.odyssi.mms.appstore.apple.AppleAppDetailsResponse
    at org.jboss.resteasy.client.core.BaseClientResponse.createResponseFailure(BaseClientResponse.java:523)
    at org.jboss.resteasy.client.core.BaseClientResponse.createResponseFailure(BaseClientResponse.java:514)
    at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:415)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:377)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:350)
    at org.jboss.resteasy.client.core.extractors.BodyEntityExtractor.extractEntity(BodyEntityExtractor.java:62)
    at org.jboss.resteasy.client.core.ClientInvoker.invoke(ClientInvoker.java:126)
    at org.jboss.resteasy.client.core.ClientProxy.invoke(ClientProxy.java:88)
    at $Proxy21.lookupByID(Unknown Source)
    at net.odyssi.mms.appstore.apple.test.AppleAppStoreLookupClientTest.testLookupByID(AppleAppStoreLookupClientTest.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我正在运行RestEasy 2.3.6。任何想法可能导致这种错误吗?

参考方案

您的类路径上有json提供程序吗?如果使用的是maven,请尝试添加以下依赖项:

 <dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jettison-provider</artifactId>
    <version>2.3.6.Final</version>
</dependency>

http://docs.jboss.org/resteasy/docs/2.3.6.Final/userguide/html_single/#JAXB_+_JSON_provider

编辑:

通常,将使用内容类型application/json而不是text/javascript。我不确定您为什么使用text/javascript

无论如何,如果端点仅返回text/javascript,则始终可以在拦截器中修改Content-Type -header:

ResteasyProviderFactory factory = new ResteasyProviderFactory();
RegisterBuiltin.register(factory);
factory.getClientExecutionInterceptorRegistry().register(
    new ClientExecutionInterceptor() {
        @Override
        public ClientResponse execute(ClientExecutionContext ctx) throws Exception {
            ClientResponse response = ctx.proceed();
            if("text/javascript".equals(response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE))){
                response.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
            }
            return response;
        }
    });


ProxyFactory.create(Service.class, URI.create("http://url-to-your-sevice"), new ApacheHttpClient4Executor(), factory);

RestEasy:如何使用参数和api键发送简单的POST请求 - java

我是一个初学者,我对Resteasy有点迷失我想使用以下网址发送帖子请求:http://myurl.com/options?value=3name=pictureString myValue = "3"; String myName = "picture"; String key = "topsecret&#…

Java:线程池如何将线程映射到可运行对象 - java

试图绕过Java并发问题,并且很难理解线程池,线程以及它们正在执行的可运行“任务”之间的关系。如果我创建一个有10个线程的线程池,那么我是否必须将相同的任务传递给池中的每个线程,或者池化的线程实际上只是与任务无关的“工人无人机”可用于执行任何任务?无论哪种方式,Executor / ExecutorService如何将正确的任务分配给正确的线程? 参考方案 …

JAVA:字节码和二进制有什么区别? - java

java字节代码(已编译的语言,也称为目标代码)与机器代码(当前计算机的本机代码)之间有什么区别?我读过一些书,他们将字节码称为二进制指令,但我不知道为什么。 参考方案 字节码是独立于平台的,在Windows中运行的编译器编译的字节码仍将在linux / unix / mac中运行。机器代码是特定于平台的,如果在Windows x86中编译,则它将仅在Win…

java:继承 - java

有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…

Java:BigInteger,如何通过OutputStream编写它 - java

我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…