Java自帶的Http?Server實現設置返回值的類型(content-type)
Http Server如何設置返回值的類型(content-type)
以返回Json格式的數據為例
首先我們要自己寫一個請求的處理器,需要實現HttpHandler接口,并重寫handle方法。
handle方法會獲得一個HttpExchange,通過exchange可以設置返回值內容與長度
private void response(HttpExchange exchange, String jsonBody) { try { byte[] bytes = jsonBody.getBytes(StandardCharsets.UTF_8); exchange.getResponseHeaders().add("Content-Type", "application/json; charset=utf-8"); exchange.sendResponseHeaders(200, bytes.length); OutputStream outputStream = exchange.getResponseBody(); outputStream.write(jsonBody.getBytes(StandardCharsets.UTF_8)); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
注意兩條語句的順序:
設置請求頭的語句一定要放在sendResponseHeaders前面!
sendResponse主要作用是發(fā)送響應頭給客戶端,發(fā)送后再設置響應頭會被忽略掉。
也就是說響應頭和響應體是分開發(fā)送的。這個可以通過debug驗證,如下:
斷點打在這里,用postman發(fā)起請求
postman顯示發(fā)送狀態(tài)
繼續(xù)向下執(zhí)行,設置請求頭,依然是上述狀態(tài)。
直到執(zhí)行完sendResponseHeaders,postman雖然還在等待數據,但是響應頭已經接收到了:
響應頭
response body
執(zhí)行到最后,發(fā)送完響應體后,截圖如下:
最后結果
由此可見響應頭與響應體是分開發(fā)送的。
獲取http接口返回值
? ? /** ? ? ?*description:獲取http接口返回值 有參數 ? ? ?* 返回值為 jsonarray 字符串形式 ? ? ?*@author: fuxb ? ? ?*@date: 2021/7/28 11:00 ? ? ?@param path ?http 接口地址 ? ? ?@param post ?參數 形式為 ?param1=value1¶m2=value2 ? ? ?*@return: com.google.gson.JsonObject ? ? ?*/ ? ? public static String postDownloadJson(String path,String post){ ? ? ? ? URL url = null; ? ? ? ? try { ? ? ? ? ? ? url = new URL(path); ? ? ? ? ? ? HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); ? ? ? ? ? ? httpURLConnection.setRequestMethod("POST");// 提交模式 ? ? ? ? ? ? // conn.setConnectTimeout(10000);//連接超時 單位毫秒 ? ? ? ? ? ? // conn.setReadTimeout(2000);//讀取超時 單位毫秒 ? ? ? ? ? ? // 發(fā)送POST請求必須設置如下兩行 ? ? ? ? ? ? httpURLConnection.setDoOutput(true); ? ? ? ? ? ? httpURLConnection.setDoInput(true); ? ? ? ? ? ? // 獲取URLConnection對象對應的輸出流 ?解決java.io.IOException: Server returned HTTP response code: 500 ?問題,使用OutputStreamWriter 轉 utf-8格式,參數不能有空格的問題 // ? ? ? ? ? ?PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream()); ? ? ? ? ? ? OutputStreamWriter printWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8"); ? ? ? ? ? ? // 發(fā)送請求參數 ? ? ? ? ? ? java.net.URLEncoder.encode(post, "UTF-8"); ? ? ? ? ? ? printWriter.write(post);//post的參數 xx=xx&yy=yy ? ? ? ? ? ? // flush輸出流的緩沖 ? ? ? ? ? ? printWriter.flush(); ? ? ? ? ? ? //開始獲取數據 ? ? ? ? ? ? BufferedInputStream bis = new ? BufferedInputStream(httpURLConnection.getInputStream()); ? ? ? ? ? ? ByteArrayOutputStream bos = new ByteArrayOutputStream(); ? ? ? ? ? ? int len; ? ? ? ? ? ? byte[] arr = new byte[1024]; ? ? ? ? ? ? while((len=bis.read(arr))!= -1){ ? ? ? ? ? ? ? ? bos.write(arr,0,len); ? ? ? ? ? ? ? ? bos.flush(); ? ? ? ? ? ? } ? ? ? ? ? ? bos.close(); // ? ? ? ? ? ?return (JsonObject)parse.parse(bos.toString("utf-8")); ? ? ? ? ? ? return bos.toString("utf-8"); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? return null; ? ? }
//返回的json數組字符串轉list<bean>: List<ResKPID> listKpid = JSONObject.parseArray(listStrKpid, ResKPID.class);
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot結合Mybatis實現創(chuàng)建數據庫表的方法
本文主要介紹了SpringBoot結合Mybatis實現創(chuàng)建數據庫表的方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01springboot+redis實現微博熱搜排行榜的示例代碼
本文主要介紹了springboot+redis實現微博熱搜排行榜的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05SpringBoot根據注解動態(tài)執(zhí)行類中的方法實現
本文主要介紹了SpringBoot根據注解動態(tài)執(zhí)行類中的方法實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-08-08