• 非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

  • 相关阅读:
    MFC开发编程规范(二)
    Mysql日期和时间函数大全(转)
    php获取客户端IP地址的几种方法
    postgres 查看数据表和索引的大小
    PHP应用memcache函数详解
    css自动截取文字 兼容IE firefox Opera
    JavaScript在IE和Firefox浏览器下的7个差异兼容写法小结
    Zend_Auth与Zend_Acl访问控制链
    去除所有js,html,css代码问题
    [转]那些相见恨晚的 JavaScript 技巧
  • 原文地址:https://www.cnblogs.com/coder-wzr/p/11464879.html
Copyright © 2020-2023  润新知