非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依赖

复制代码
<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/94928352

posted on   幕码人  阅读(1320)  评论(0)    收藏  举报

编辑推荐:
· 编码之道,道心破碎。
· 记一次 .NET 某发证机系统 崩溃分析
· 微服务架构学习与思考:SOA架构与微服务架构对比分析
· tomcat为什么假死了
· 聊一聊 Linux 上对函数进行 hook 的两种方式
阅读排行:
· 编码之道,道心破碎。
· 知名开源项目Alist被收购!惹程序员众怒,开团炮轰甲方
· 如何给 GitHub Copilot "洗脑”,让 AI 精准遵循指令产出高质量代码
· 突发,小红书开发者后门被破解?!
· 历时半年,我将一个大型asp.net的零代码快速开发平台转成了java
< 2025年6月 >
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 1 2 3 4 5
6 7 8 9 10 11 12

导航

统计

点击右上角即可分享
微信分享提示