Eclipse调用RestAPI报400 Bad Request,Postman可正常运行
问题:Java调用REST接口返回400 Bad Request,但Postman可正常调用
我编写了一个Java函数,接收两个输入参数:
- JSON文件路径
- REST接口URL
调用该函数后应返回JSON格式结果,但在Eclipse中运行时抛出400错误,相同接口在Postman中可正常调用。
错误日志
Error Log : URL: http://localhost:26081/jderest/v3/orchestrator/ORC_2212160001JDE Conn : sun.net.www.protocol.http.HttpURLConnection:http://localhost:26081/jderest/v3/orchestrator/ORC_2212160001JDE JSON Input path : \\phxxxx155826.xxxxxx.fusionappsdphx1.xxxxxxxx.com\\FF\\Orchest\\orc1.json { "username": "ic8823790", "password": "ic8823790", "Product_No": "sss265", "Description": "sss265", "Stocking_Type": "s", "G_L_Class": "in30", "Unit_of_Measure": "ea", "Search_Text": "sss265", "Branch_Plant": "30" } HTTP CODE : 400 java.io.IOException: Server returned HTTP response code: 400 for URL: http://exx11001.ad1.xxxxxxxdephx.xxxxxxx.com:26081/jderest/v3/orchestrator/ORC_2212160001JDE at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1944) at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1939) at java.security.AccessController.doPrivileged(Native Method) at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1938) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1508) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492) at OrchestratorAPI.orchestratorCall(OrchestratorAPI.java:108) at OrchestratorAPI.main(OrchestratorAPI.java:25) Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://ems11001.ad1.fusionappsdephx.oraclevcn.com:26081/jderest/v3/orchestrator/ORC_2212160001JDE at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492) at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480) at OrchestratorAPI.orchestratorCall(OrchestratorAPI.java:100) ... 1 more
我的代码
public class OrchestratorAPI { public static void main(String[] args) throws MalformedURLException, IOException { // TODO Auto-generated method stub String filepath = "\\\\xxxxxxx155826.xxxxxxx.fusionappsdphx1.xxxxxxx.com\\FF\\Orchest\\orc1.json"; String orchAPI = "http://exx11001.ad1.xxxxxxxdephx.xxxxxxx.com:26081/jderest/v3/orchestrator/ORC_2212160001JDE"; orchestratorCall(filepath, orchAPI); } public static void orchestratorCall(String filePath, String orchestAPI) throws MalformedURLException, IOException { String charset = "UTF-8"; StringBuilder result; String content = ""; String path = "", sid = ""; String paramsJSON=""; URLConnection con = new URL(orchestAPI).openConnection(); System.out.println("URL : "+orchestAPI + "\nConn : "+con); System.out.println("JSON Input path : " + filePath); try { paramsJSON = new String( Files.readAllBytes(Paths.get(filePath))); } catch (IOException e) { e.printStackTrace(); } System.out.println(paramsJSON); try { ((HttpURLConnection) con).setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/json; UTF-8"); con.setRequestProperty("Accept", "application/json"); con.setDoOutput(true); con.setReadTimeout(60000); con.setConnectTimeout(60000); try (OutputStream os = con.getOutputStream()) { byte[] input = paramsJSON.getBytes(charset); os.write(input, 0, input.length); } int code = ((HttpURLConnection) con).getResponseCode(); System.out.println("HTTP CODE : " + String.valueOf(code)); } catch (IOException e) { e.printStackTrace(); } try { // Receive the response from the server InputStream in = new BufferedInputStream(con.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); result = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { result.append(line); } System.out.println("JSON Parser -> result: " + result.toString()); try { // Create new report file content = result.toString(); path = "\\\\xxxxxxx155826.xxxxxxx.fusionappsdphx1.xxxxxxxx.com\\FF\\Orchest\\" + "" + "OrchestratorReport.json"; System.out.println("Home Dir path : " + path); File file = new File(path); // If file doesn't exists, then create it if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); // Write in file bw.write(content); // Close connection bw.close(); } catch (Exception e) { System.out.println(e); } } catch (IOException e) { e.printStackTrace(); } ((HttpURLConnection) con).disconnect(); } }
排查建议
- 核对请求头差异:对比Postman与Java代码的请求头,比如
Content-Type格式(代码中是application/json; UTF-8,Postman可能仅为application/json),检查是否遗漏User-Agent等必填请求头。 - 修正JSON编码读取:代码中读取JSON文件时未指定编码,可能导致字符乱码,修改为:
paramsJSON = new String(Files.readAllBytes(Paths.get(filePath)), charset); - 获取服务器详细错误信息:400错误时,服务器通常会返回具体错误描述,需读取错误流而非仅输入流,修改响应处理逻辑:
int code = ((HttpURLConnection) con).getResponseCode(); System.out.println("HTTP CODE : " + code); InputStream in; if (code >= 200 && code < 300) { in = con.getInputStream(); } else { in = ((HttpURLConnection) con).getErrorStream(); } // 后续读取流的逻辑不变
- 确认URL一致性:错误日志中出现了
localhost与exx11001.ad1.xxxxxxxdephx.xxxxxxx.com两个不同URL,确保代码使用的URL与Postman完全一致。 - 对比请求体内容:将代码打印的
paramsJSON与Postman发送的请求体逐字符对比,确认格式、空格、换行等细节完全一致。
内容的提问来源于stack exchange,提问作者BipLab
相关产品推荐
相关产品推荐

