Java REST ful 客户端代码有问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7117202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:41:54  来源:igfitidea点击:

Java REST ful client code is problem

javarest

提问by Mohan

I am trying to create the REST-ful web service in java. This REST-ful web service contains both Hibernate configuration and Rest-ful configuration. Actually the java web service is working perfect. but when i am try to execute the client code for that web service.

我正在尝试在 java 中创建 REST-ful web 服务。这个 REST-ful web 服务包含 Hibernate 配置和 Rest-ful 配置。实际上,java Web 服务运行良好。但是当我尝试执行该 Web 服务的客户端代码时。

The jar files that am using is

正在使用的 jar 文件是

activation
antlr-2.7.6
asm-3.1
asm-attrs
cglib-nodep
commons-collections-2.1.1
commons-fileupload-1.2.1
commons-io-1.3.2
commons-logging-1.1
dom4j-1.6.1
ehcache-1.2.3
ejb3-persistence
grizzly-servlet-webserver-1.7.3.2
hibernate-annotations
hibernate-commons-annotations
hibernate-entitymanager
hibernate-tools
hibernate3
http
javassist
jaxb-api
jaxb-impl
jaxb-xjc
jdbc2_0-stdext
jdom-1.0
jersey-core-1.7
jersey-server-1.7
jersey-spring
jersey
jettison-1.0-RC1
jsr173_api
jsr311-api
jta
mysql-connector-java-5.1.12-bin
osgi
rome-0.9
wadl2java

My problem is

我的问题是

Exception in thread "main" java.lang.NoSuchMethodError: com.sun.jersey.spi.inject.InjectableProviderContext.getInjectable(Ljava/lang/Class;Lcom/sun/jersey/spi/service/ComponentContext;Ljava/lang/annotation/Annotation;Ljava/lang/Object;Ljava/util/List;)Lcom/sun/jersey/spi/inject/Injectable;
        at com.sun.jersey.spi.service.ComponentConstructor.getConstructor(ComponentConstructor.java:139)
        at com.sun.jersey.impl.application.ComponentProviderCache.getComponent(ComponentProviderCache.java:177)
        at com.sun.jersey.impl.application.ComponentProviderCache.getProvidersAndServices(ComponentProviderCache.java:122)
        at com.sun.jersey.impl.application.MessageBodyFactory.getProviderMap(MessageBodyFactory.java:133)
        at com.sun.jersey.impl.application.MessageBodyFactory.initReaders(MessageBodyFactory.java:107)
        at com.sun.jersey.impl.application.MessageBodyFactory.init(MessageBodyFactory.java:102)
        at com.sun.jersey.api.client.Client.<init>(Client.java:284)
        at com.sun.jersey.api.client.Client.<init>(Client.java:209)
        at com.sun.jersey.api.client.Client.create(Client.java:429)
        at pack1.client.Test.main(Test.java:20)

Client Java code is

客户端 Java 代码是

package pack1.client;

import java.net.URI;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class Test {
    public static void main(String[] args) {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource service = client.resource(getBaseURI());
        System.out.println(service.path("resources").path("hello").path("xml").accept(
                        MediaType.TEXT_PLAIN).get(ClientResponse.class));
    }
    private static URI getBaseURI() {
            return UriBuilder.fromUri("http://localhost/WebServicePgm1").build();
    }
}

The method that am trying to test in client code which is

我试图在客户端代码中测试的方法是

    @GET
    @Path("xml")
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey </hello>";
    }

After Changing the jar file

更改jar文件后

the above problem is solved and new problem gets occured.

上面的问题解决了,新的问题又出现了。

@ gkatkov am using Apache Tomcat 6.0.20 and now. i have updated the jersey jar to jersey 1.8. after updating the jar file am getting another error that is

@ gkatkov 现在使用的是 Apache Tomcat 6.0.20。我已将球衣罐更新为球衣 1.8。更新 jar 文件后,我收到另一个错误

Jar file that i put now is

我现在放的 Jar 文件是

jersey-server-1.8.jar
jersey-json-1.8.jar
jersey-core-1.8.jar
jersey-client-1.8.jar

Now the problem is

现在的问题是

com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for field: javax.ws.rs.core.UriInfo com.sun.jersey.impl.template.ViewableMessageBodyWriter.ui
  SEVERE: Missing dependency for field: com.sun.jersey.spi.template.TemplateContext com.sun.jersey.impl.template.ViewableMessageBodyWriter.tc
Exception in thread "main" com.sun.jersey.spi.inject.Errors$ErrorMessagesException
        at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
        at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
        at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
        at com.sun.jersey.api.client.Client.<init>(Client.java:187)
        at com.sun.jersey.api.client.Client.<init>(Client.java:170)
        at com.sun.jersey.api.client.Client.create(Client.java:679)
        at pack1.client.Test.main(Test.java:20)

do i need to put any other jar files?

我需要放任何其他 jar 文件吗?

回答by Wade

I suffered the same problem. I found that using the jersey-bundle-1.8.jar instead of client and server library respectively could solve this problem. you can try it.

我遇到了同样的问题。我发现分别使用 jersey-bundle-1.8.jar 而不是客户端和服务器库可以解决这个问题。你可以试试看。