使用ClientResponse Jersey发出调用Rest API的问题

时间:2015-11-05 11:47:07

标签: java rest jersey

当我从“Advance Rest Client”镀铬扩展调用休息服务时遇到问题我得到了正确的响应,但是当我使用“ClientResponse”Jersey调用相同的API时,它给出了错误500内部服务器错误..

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

请求数据:

{ “PRODUCTCODE”: “0400087200629”, “sku_id”: “1689949379651610”, “itemQuantity”: “1”, “邮政编码”: “10010”, “半径”: “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 个答案:

答案 0 :(得分:0)

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

你可以尝试类似的东西:

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

请参阅此帖How to submit data with Jersey client POST method

的第二个答案