使用 ClientResponse Jersey 调用 Rest API

Posted

技术标签:

【中文标题】使用 ClientResponse Jersey 调用 Rest API【英文标题】:Issue calling Rest API using ClientResponse Jersey 【发布时间】:2016-02-06 05:45:53 【问题描述】:

当我从“Advance Rest Client”chrome 扩展调用 rest 服务时遇到问题,我得到了正确的响应,但是当我使用“ClientResponse”Jersey 调用相同的 API 时,它给了我错误 500 internal server error..

这是我的 API 网址:http://www.saksfifthavenue.com/main/ProductDetail.jsp

请求数据:

"productCode":"0400087200629","sku_id":"1689949379651610","itemQuantity":"1","zipCode":"10010","radius":"10","bmForm":"get_stores_with_inventory "

public class JerseyClientPost

    public static void main(String[] args)
    

        try
        

            Client client = Client.create();

            WebResource webResource = client
                    .resource("http://www.saksfifthavenue.com/main/ProductDetail.jsp");

            String input = "\"productCode\":\"0400087200629\",\"sku_id\":\"1689949379651610\",\"itemQuantity\":\"1\",\"zipCode\":\"10010\",\"radius\":\"10\",\"bmForm\":\"get_stores_with_inventory\"";

            Product product = new Product();
            product.setProductCode("0400087200629");
            product.setItemQuantity("1");
            product.setRadius("10");
            product.setSku_id("1689949379651610");
            product.setZipCode("10010");
            product.setBmForm("get_stores_with_inventory");

            ClientResponse response = webResource.post(ClientResponse.class, product);

            System.out.println("Output from Server .... \n");
            String output = response.getEntity(String.class);
            System.out.println(output);

         catch (Exception e)
        
            e.printStackTrace();
        
    

来自服务器的输出 ....

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Draft//EN">
<HTML>
<HEAD>
<TITLE>Error 500--Internal Server Error</TITLE>
<META NAME="GENERATOR" CONTENT="WebLogic Server">
</HEAD>
<BODY bgcolor="white">
<FONT FACE=Helvetica><BR CLEAR=all>
<TABLE border=0 cellspacing=5><TR><TD><BR CLEAR=all>
<FONT FACE="Helvetica" COLOR="black" SIZE="3"><H2>Error 500--Internal Server Error</H2>
</FONT></TD></TR>
</TABLE>
<TABLE border=0 width=100% cellpadding=10><TR><TD VALIGN=top WIDTH=100% BGCOLOR=white><FONT FACE="Courier New"><pre>


java.lang.IllegalArgumentException: malformed query string
    at com.bluemartini.http.QueryParams.parseQueryOrPost(QueryParams.java:916)
    at com.bluemartini.http.QueryParams.parsePostData(QueryParams.java:844)
    at com.bluemartini.http.QueryParams.parsePostData(QueryParams.java:823)
    at com.bluemartini.http.QueryParams.parsePostData(QueryParams.java:806)
    at com.bluemartini.http.QueryParams.<init>(QueryParams.java:436)
    at com.bluemartini.html.StandardRequestHandler.createFormValues(StandardRequestHandler.java:461)
    at com.bluemartini.html.StandardRequestHandler.init(StandardRequestHandler.java:244)
    at com.bluemartini.html.HTMLFilter.doFilter(HTMLFilter.java:340)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:42)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3496)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(Unknown Source)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2180)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2086)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1406)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
</pre></FONT></TD></TR>
</TABLE>

</BODY>
</HTML>

【问题讨论】:

你有错误的堆栈跟踪吗? 【参考方案1】:

也许服务器不知道您正在发送 JSON。

你可以试试这样的:

ClientResponse response = webResource.accept("application/json")
                .type("application/json").post(ClientResponse.class, product);

查看此帖子的第二个答案How to submit data with Jersey client POST method

【讨论】:

以上是关于使用 ClientResponse Jersey 调用 Rest API的主要内容,如果未能解决你的问题,请参考以下文章

泛型类型的 Jersey ClientResponse.getEntity

aiohttp 异步http请求-11.ClientResponse 获取响应headers 和cookies

如何从 Mono<ClientResponse> 获取正文?

REST客户端Content-Type标头抛出UnsupportedOperationException

如何返回改造时使用的方法OnResponse的列表?

将使用 jersey 1.x 的 java 代码转换为 jersey 2.x