简体   繁体   中英

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. I referred one of the springs webapp which does the same using autowiring. I could see the below in the spring config file of the webapp.

<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. Is my understand correct?

I also see the below usage in the webapp code.

@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):

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.

Finally I decided to go with HttpClient and it worked fine. Also I decided to use Gson from Google to do the JSON to Java transformation. My program is now working fine. More info about Gson can be found at 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. At runtime the util:list translates to a list behind a spring proxy. 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.

Why do you programmatically create the jsonprovider and webclient? May be you have a reason but making it configurable is more 'reusable'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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