简体   繁体   English

从命令行程序访问REST服务的问题

[英]Issues with accessing REST service from command line program

I am trying to create a command line Java program which has to access some REST services. 我正在尝试创建一个命令行Java程序,它必须访问一些REST服务。 I referred one of the springs webapp which does the same using autowiring. 我推荐了一个使用自动装配的弹簧webapp。 I could see the below in the spring config file of the webapp. 我可以在webapp的spring配置文件中看到以下内容。

<bean id="jacksonJsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />

<util:list id="webClientProviders">     
    <ref bean="jacksonJsonProvider"/>
</util:list> 

<bean id="jsonWebClient" class="org.apache.cxf.jaxrs.client.WebClient" factory-method="create">
    <constructor-arg type="java.lang.String" value="http://localhost:8080/"/> 
    <constructor-arg ref="webClientProviders" /> 
</bean>

This tells me that spring will create an instance of WbClient using the arguments ' http://localhost:8080/ ' and a List having an instance of JacksonJsonProvider. 这告诉我spring将使用参数' http:// localhost:8080 / '和一个具有JacksonJsonProvider实例的List创建一个WbClient实例。 Is my understand correct? 我的理解是否正确?

I also see the below usage in the webapp code. 我还在webapp代码中看到了以下用法。

@Controller
public class ABController {
    @Autowired
    @Qualifier("jsonWebClient")
    private WebClient webclient;

    @RequestMapping(value = "/abc.action", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public String getABCD(HttpServletRequest request, HttpServletResponse response) {
        ...
        ...
        WebClient wc = WebClient.create(webclient.getBaseURI());
        wc.path("abcdservices/rest/restservices/cart/gettotal");
        Response res = wc.get();
        ...
        ...
    }
}

But when I do the same in my Java program, as shown below (and some variants): 但是当我在我的Java程序中执行相同操作时,如下所示(以及一些变体):

List<Object> providers = new ArrayList<Object>();
JacksonJsonProvider j = new JacksonJsonProvider();
providers.add(j);
WebClient webclient = WebClient.create("http://localhost:8080/", 
            providers);
WebClient wc = webclient.create(webclient.getBaseURI());
wc.path("crmitsm/rest/cirestservices/crmitsm/warrantystatus");
Response res = wc.get();

I get the below exception / error. 我得到以下异常/错误。

java -jar target/CmdLine-0.0.1-SNAPSHOT-jar-with-dependencies.jar

Exception in thread "main" java.lang.NullPointerException
    at org.apache.cxf.jaxrs.client.AbstractClient.setupOutInterceptorChain(AbstractClient.java:887)
    at org.apache.cxf.jaxrs.client.AbstractClient.createMessage(AbstractClient.java:958)
    at org.apache.cxf.jaxrs.client.WebClient.finalizeMessage(WebClient.java:1118)
    at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1091)
    at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:894)
    at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:865)
    at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:331)
    at org.apache.cxf.jaxrs.client.WebClient.get(WebClient.java:357)
    at org.CmdLine.App.main(App.java:37)

Could anybody please help me here. 有人可以帮我。 I am not able to understand what I am missing here. 我无法理解我在这里缺少的东西。

The problem I had posted could be some dependency issue. 我发布的问题可能是一些依赖问题。 I tried multiple solutions posted for similar problems - all being adding more dependencies, tried with shred plugin instead of assembly etc., but none worked. 我尝试了针对类似问题发布的多个解决方案 - 所有都是添加更多依赖项,尝试使用shred插件而不是汇编等,但没有一个工作。

Finally I decided to go with HttpClient and it worked fine. 最后我决定使用HttpClient,它工作正常。 Also I decided to use Gson from Google to do the JSON to Java transformation. 我还决定使用Google的Gson来进行JSON到Java的转换。 My program is now working fine. 我的程序现在运行正常。 More info about Gson can be found at Converting JSON to Java . 有关Gson的更多信息,请参阅Converting JSON to Java

The util:List you see in spring config is not exactly the same as a java.util.List you used in your code. 您在spring config中看到的util:List与您在代码中使用的java.util.List不完全相同。 At runtime the util:list translates to a list behind a spring proxy. 在运行时,util:list转换为spring代理后面的列表。 You can print out its class and verify this. 您可以打印出它的类并验证这一点。 So the WebClient creation fails with NullPointerException because spring probably expects a proxy where you passed a juList. 因此,WebClient创建失败并出现NullPointerException,因为spring可能需要一个传递juList的代理。

Why do you programmatically create the jsonprovider and webclient? 为什么以编程方式创建jsonprovider和webclient? May be you have a reason but making it configurable is more 'reusable'. 可能你有理由,但让它可配置更“可重用”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM