Java 在同一个tomcat服务器中使用Apache Http客户端发送Json字符串

Java 在同一个tomcat服务器中使用Apache Http客户端发送Json字符串,java,json,tomcat,apache-httpclient-4.x,Java,Json,Tomcat,Apache Httpclient 4.x,我正在尝试使用apache http客户端向服务器发送json。我已经尝试使用Gson和jackson API创建json字符串,我正在使用这两种方法获取有效的json字符串,但我面临的问题是使用apache http客户端发送json时,我收到了错误的请求。是json字符串被转换成类似于控制台上看到的无效json的问题还是其他问题。这是我的代码 objAuthRequest.setModality(objFITransXTRequest.getModality()); objA

我正在尝试使用apache http客户端向服务器发送json。我已经尝试使用Gson和jackson API创建json字符串,我正在使用这两种方法获取有效的json字符串,但我面临的问题是使用apache http客户端发送json时,我收到了错误的请求。是json字符串被转换成类似于控制台上看到的无效json的问题还是其他问题。这是我的代码

objAuthRequest.setModality(objFITransXTRequest.getModality());
        objAuthRequest.setUidNumber(objFITransXTRequest.getUidNumber());
        objAuthRequest.setRrn("123456789133");
        objAuthRequest.setStan("12345600");
        objAuthRequest.setPid(objFITransXTRequest.getPid());
        objAuthRequest.setHmac(objFITransXTRequest.getHmac());
        objAuthRequest.setCi(objFITransXTRequest.getCi());
        objAuthRequest.setsKey(objFITransXTRequest.getsKey());
        objAuthRequest.setMetaData(objFITransXTRequest.getMetaData());

        try {

           Gson objGson = new Gson();
            String strRequestJson = objGson.toJson(objAuthRequest);
            System.out.println("Request json " + strRequestJson);

            response = objMWTHttpClient.execute(strRequestJson, URL);
            System.out.println("Response json " + response);
        } catch (Exception e) {
            logger.error("## Exception occured :: "+e.getMessage());
        }
        return response;
这是我的apache客户端代码

public String execute(String authenticateData, String srtULR) throws ParseException, Exception {
        String resoutput = "";
        try {
            HttpClient httpClient = HttpClientBuilder.create().build();
            //HttpPut reqFundTransf = new HttpPut(srtULR);
            HttpPost reqFundTransf = new HttpPost(srtULR);

            StringEntity params = new StringEntity(authenticateData);
            reqFundTransf.addHeader("content-type", "application/json");
            reqFundTransf.setEntity(params);
            HttpResponse resp = httpClient.execute(reqFundTransf);
            HttpEntity respEntity = resp.getEntity();
            if (respEntity != null) {
                resoutput = EntityUtils.toString(respEntity);
            }

        } catch (Exception e) {
            throw new Exception(e);
        }
        return resoutput;
} 
在控制台上,我收到这些消息

Request json {"modality":{"fp":true,"otp":false},"uidNumber":"49664043668","rrn":"123456789133","stan":"12345600","pid":"YUVIL7CKWbBjFV+UCZBKGMnLJeHSNJjrZ6r+0kyfMZl0VEKNeIHyeaXFPLsYXyxJ9qISBTPmN/4qrS82AT781Q\u003d\u003d","hmac":"XuVPiZ9kLxHSAzxzyFqu+eY18F1bQVqOzjbGF7Pc21L2ohIFM+Y3/iqtLzYBPvzV","ci":"20170227","sKey":"lrq66RF9C0H00tFVA3MjRRui0NFyM/1BlYxfJkzHFZhrWaLt1xpUd7M1z1skeXqJwNmB7Ygcyht22Tqr701eHSiZTKWl6N4gJLDr4EZjDB+OYqDTg28qSiHC3/TDvASr9m+F0ymKZhAdw7aASf+ZCxpkdjBOI/FzaIlTo67r9azYeVKPd/53bnpOvboxUdpt0coI8ElheTArcE5xQVDVC3Y5iJYkARjVgRonrjWSLlb0D9WzZ2Aolq2vnwo7nnglb0uqiqanUUrStqK//MNcVq5R5acu5hZv83XZNH0m/4v1+Ku3zQp0Kuc6nRNkE0M6f+LKZJ8JlkeSnIwoubFA1Q\u003d\u003d","metaData":{"udc":"AX20123","pip":"10.230.23.24","lot":"P","lov":"244221"}}
2017-02-23 13:11:20 DEBUG RequestAddCookies:122 - CookieSpec selected: best-match
2017-02-23 13:11:20 DEBUG RequestAuthCache:75 - Auth cache not set in the context
2017-02-23 13:11:20 DEBUG PoolingHttpClientConnectionManager:219 - Connection request: [route: {}->http://localhost:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
2017-02-23 13:11:20 DEBUG PoolingHttpClientConnectionManager:250 - Connection leased: [id: 0][route: {}->http://localhost:8080][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
2017-02-23 13:11:20 DEBUG MainClientExec:217 - Opening connection {}->http://localhost:8080
2017-02-23 13:11:20 DEBUG HttpClientConnectionOperator:120 - Connecting to localhost/127.0.0.1:8080
2017-02-23 13:11:20 DEBUG HttpClientConnectionOperator:127 - Connection established 127.0.0.1:46299<->127.0.0.1:8080
2017-02-23 13:11:20 DEBUG MainClientExec:238 - Executing request POST /AEPSNPCI/rest/aepsauth/authentication HTTP/1.1
2017-02-23 13:11:20 DEBUG MainClientExec:243 - Target auth state: UNCHALLENGED
2017-02-23 13:11:20 DEBUG MainClientExec:249 - Proxy auth state: UNCHALLENGED
2017-02-23 13:11:20 DEBUG headers:124 - http-outgoing-0 >> POST /AEPSNPCI/rest/aepsauth/authentication HTTP/1.1
2017-02-23 13:11:20 DEBUG headers:127 - http-outgoing-0 >> Content-Length: 737
2017-02-23 13:11:20 DEBUG headers:127 - http-outgoing-0 >> Content-Type: application/json; charset=UTF-8
2017-02-23 13:11:20 DEBUG headers:127 - http-outgoing-0 >> Host: localhost:8080
2017-02-23 13:11:20 DEBUG headers:127 - http-outgoing-0 >> Connection: Keep-Alive
2017-02-23 13:11:20 DEBUG headers:127 - http-outgoing-0 >> User-Agent: Apache-HttpClient/4.3.4 (java 1.5)
2017-02-23 13:11:20 DEBUG headers:127 - http-outgoing-0 >> Accept-Encoding: gzip,deflate
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 >> "POST /AEPSNPCI/rest/aepsauth/authentication HTTP/1.1[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 >> "Content-Length: 737[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 >> "Content-Type: application/json; charset=UTF-8[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 >> "Host: localhost:8080[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.3.4 (java 1.5)[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 >> "[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:86 - http-outgoing-0 >> "{"modality":{"fp":true,"otp":false},"uidNumber":"49664043668","rrn":"123456789133","stan":"12345600","pid":"YUVIL7CKWbBjFV+UCZBKGMnLJeHSNJjrZ6r+0kyfMZl0VEKNeIHyeaXFPLsYXyxJ9qISBTPmN/4qrS82AT781Q\u003d\u003d","hmac":"XuVPiZ9kLxHSAzxzyFqu+eY18F1bQVqOzjbGF7Pc21L2ohIFM+Y3/iqtLzYBPvzV","ci":"20170227","sKey":"lrq66RF9C0H00tFVA3MjRRui0NFyM/1BlYxfJkzHFZhrWaLt1xpUd7M1z1skeXqJwNmB7Ygcyht22Tqr701eHSiZTKWl6N4gJLDr4EZjDB+OYqDTg28qSiHC3/TDvASr9m+F0ymKZhAdw7aASf+ZCxpkdjBOI/FzaIlTo67r9azYeVKPd/53bnpOvboxUdpt0coI8ElheTArcE5xQVDVC3Y5iJYkARjVgRonrjWSLlb0D9WzZ2Aolq2vnwo7nnglb0uqiqanUUrStqK//MNcVq5R5acu5hZv83XZNH0m/4v1+Ku3zQp0Kuc6nRNkE0M6f+LKZJ8JlkeSnIwoubFA1Q\u003d\u003d","metaData":{"udc":"AX20123","pip":"10.230.23.24","lot":"P","lov":"244221"}}"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 << "HTTP/1.1 400 Bad Request[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 << "Server: Apache-Coyote/1.1[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 << "Transfer-Encoding: chunked[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 << "Date: Thu, 23 Feb 2017 07:41:20 GMT[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 << "Connection: close[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 << "[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 << "0[\r][\n]"
2017-02-23 13:11:20 DEBUG wire:72 - http-outgoing-0 << "[\r][\n]"
2017-02-23 13:11:20 DEBUG headers:113 - http-outgoing-0 << HTTP/1.1 400 Bad Request
2017-02-23 13:11:20 DEBUG headers:116 - http-outgoing-0 << Server: Apache-Coyote/1.1
2017-02-23 13:11:20 DEBUG headers:116 - http-outgoing-0 << Transfer-Encoding: chunked
2017-02-23 13:11:20 DEBUG headers:116 - http-outgoing-0 << Date: Thu, 23 Feb 2017 07:41:20 GMT
2017-02-23 13:11:20 DEBUG headers:116 - http-outgoing-0 << Connection: close
2017-02-23 13:11:20 DEBUG DefaultManagedHttpClientConnection:87 - http-outgoing-0: Shutdown connection
2017-02-23 13:11:20 DEBUG MainClientExec:126 - Connection discarded
2017-02-23 13:11:20 DEBUG DefaultManagedHttpClientConnection:79 - http-outgoing-0: Close connection
2017-02-23 13:11:20 DEBUG PoolingHttpClientConnectionManager:286 - Connection released: [id: 0][route: {}->http://localhost:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20] 
当我测试这个即将过时的json字符串时,它是无效的,这意味着语法不正确。我已经检查了java对象中的每个字段,一切看起来都很好。请指导怎么做。谢谢


编辑:我正在将请求从一个项目发送到同一服务器中的另一个项目。

错误请求是一个HTTP错误,它意味着您尝试了一个不正确的请求。你确定你必须使用POST吗?(可能是PUT?)或者也检查
内容类型
,确保它是
application/json
。是的,它是application/json,方法是post,但json的语法是正确的。您确定问题不在接收服务器端吗?你真的能通过第三方http客户端(cUrl,postman…)成功发布json吗?@Jeremy Grand,谢谢你提到postman。现在我从postman直接点击得到403状态“访问指定资源已被禁止”。我该怎么解决呢?嗯,您必须调查服务器为什么会限制对资源的访问。那是另一回事。没有你申请的任何信息,我无法神奇地解决你的问题。
 http-outgoing-0 >> "{"modality":{"fp":true,"otp":false},"uidNumber":"49664043668","rrn":"123456789133","stan":"12345600","pid":"YUVIL7CKWbBjFV+UCZBKGMnLJeHSNJjrZ6r+0kyfMZl0VEKNeIHyeaXFPLsYXyxJ9qISBTPmN/4qrS82AT781Q\u003d\u003d","hmac":"XuVPiZ9kLxHSAzxzyFqu+eY18F1bQVqOzjbGF7Pc21L2ohIFM+Y3/iqtLzYBPvzV","ci":"20170227","sKey":"lrq66RF9C0H00tFVA3MjRRui0NFyM/1BlYxfJkzHFZhrWaLt1xpUd7M1z1skeXqJwNmB7Ygcyht22Tqr701eHSiZTKWl6N4gJLDr4EZjDB+OYqDTg28qSiHC3/TDvASr9m+F0ymKZhAdw7aASf+ZCxpkdjBOI/FzaIlTo67r9azYeVKPd/53bnpOvboxUdpt0coI8ElheTArcE5xQVDVC3Y5iJYkARjVgRonrjWSLlb0D9WzZ2Aolq2vnwo7nnglb0uqiqanUUrStqK//MNcVq5R5acu5hZv83XZNH0m/4v1+Ku3zQp0Kuc6nRNkE0M6f+LKZJ8JlkeSnIwoubFA1Q\u003d\u003d","metaData":{"udc":"AX20123","pip":"10.230.23.24","lot":"P","lov":"244221"}}"