非JAVA WEB項目提供Http接口調用實現

package com.monitor.app.utils;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.URI;
import java.util.Map;

public class Test {

    public static void main(String[] args) throws IOException {
        HttpServer httpServer = HttpServer.create(new InetSocketAddress(8001), 0);
        httpServer.createContext("/test", new TestHandler());
        httpServer.start();
        System.out.println("8001端口已成功啓動");
    }

    //接口接收處理器
    static class TestHandler implements HttpHandler {

        @Override
        public void handle(HttpExchange httpExchange) throws IOException {
            //返回
            String rtn="調用成功";
            httpExchange.sendResponseHeaders(200, 0);
            //獲取請求路徑
            URI requestURI = httpExchange.getRequestURI();
            System.out.println("請求路徑爲:"+requestURI);
            //獲取請求方法
            String requestMethod = httpExchange.getRequestMethod();
            System.out.println("請求方法爲:"+requestMethod);
            //獲取請求體
            InputStream requestBody = httpExchange.getRequestBody();
            InputStreamReader inputStreamReader = new InputStreamReader(requestBody);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuffer stringBuffer = new StringBuffer();
            String s = "";
            while ((s = bufferedReader.readLine()) != null) {
                stringBuffer.append(s.trim());
            }
            if("GET".equals(requestMethod)){
                String[] pathParams = requestURI.toString().split("\\?");
                String[] params = requestURI.toString().split("\\?")[1].split("&");
                for(String str : params){
                    String key = str.split("=")[0];
                    String value = str.split("=")[1];
                    System.out.println("請求參數名:" + key);
                    System.out.println("請求參數值:" + value);
                }
            }
            if("POST".equals(requestMethod)){
                //此處引入谷歌Gson框架將String轉爲Map方便獲取參數
//                Gson gson = new Gson();
//                Map map = gson.fromJson(stringBuffer.toString(), new TypeToken<Map<String, Object>>(){}.getType());
                Map map = JSON.parseObject(stringBuffer.toString());
                String requestParam = map.get("imgPath").toString();
                for (Object obj : map.keySet()){
                    System.out.println("key爲:" + obj);
                    System.out.println("value爲:" + map.get(obj));
                }
            }
            OutputStream responseBody = httpExchange.getResponseBody();
            responseBody.write(rtn.getBytes());
            responseBody.close();
        }
    }
}
View Code

postman模擬調用:
在這裏插入圖片描述
響應結果爲:
postman相應調用成功狀態碼200
在這裏插入圖片描述
在這裏插入圖片描述
注:若是前面用了gson谷歌去解析json,需添加Maven依賴java

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.2.4</version>
</dependency>
View Code

原文連接:https://blog.csdn.net/weixin_42424359/article/details/94928352json

相關文章
相關標籤/搜索