简体   繁体   中英

Issue calling Rest API using ClientResponse Jersey

Facing issue when I call rest service from "Advance Rest Client" chrome extension I am getting proper response, but when I call same API using "ClientResponse" Jersey it gives me error 500 internal server error..

Here is my API URL: http://www.saksfifthavenue.com/main/ProductDetail.jsp

Request Data:

{"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();
        }
    }
}

Output from Server ....

<!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>

Maybe the server doesn't know you are sending JSON.

you could try something like :

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

see second answer to this post How to submit data with Jersey client POST method

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