非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 2019-09-05 11:38  幕码人  阅读(1313)  评论(0编辑  收藏  举报

导航