浏览代码

松原党建中间件

wang_xy 2 年之前
当前提交
a4851db7e1

+ 65 - 0
AppInterface_jian.iml

@@ -0,0 +1,65 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
+  <component name="FacetManager">
+    <facet type="Spring" name="Spring">
+      <configuration />
+    </facet>
+    <facet type="web" name="Web">
+      <configuration>
+        <webroots />
+        <sourceRoots>
+          <root url="file://$MODULE_DIR$/src/main/java" />
+          <root url="file://$MODULE_DIR$/src/main/resources" />
+        </sourceRoots>
+      </configuration>
+    </facet>
+  </component>
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
+    <output url="file://$MODULE_DIR$/target/classes" />
+    <output-test url="file://$MODULE_DIR$/target/test-classes" />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
+      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
+      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
+      <excludeFolder url="file://$MODULE_DIR$/target" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:2.6.5" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:2.6.5" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:2.6.5" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:2.6.5" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:2.6.5" level="project" />
+    <orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.2.11" level="project" />
+    <orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.2.11" level="project" />
+    <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.36" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-to-slf4j:2.17.2" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.logging.log4j:log4j-api:2.17.2" level="project" />
+    <orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.36" level="project" />
+    <orderEntry type="library" name="Maven: jakarta.annotation:jakarta.annotation-api:1.3.5" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-core:5.3.17" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-jcl:5.3.17" level="project" />
+    <orderEntry type="library" name="Maven: org.yaml:snakeyaml:1.29" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-json:2.6.5" level="project" />
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.13.2" level="project" />
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.13.2" level="project" />
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.13.2" level="project" />
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.2" level="project" />
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.2" level="project" />
+    <orderEntry type="library" name="Maven: com.fasterxml.jackson.module:jackson-module-parameter-names:2.13.2" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:2.6.5" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.60" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:9.0.60" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:9.0.60" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-web:5.3.17" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-beans:5.3.17" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-webmvc:5.3.17" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-aop:5.3.17" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-context:5.3.17" level="project" />
+    <orderEntry type="library" name="Maven: org.springframework:spring-expression:5.3.17" level="project" />
+    <orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.13" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.4.10" level="project" />
+    <orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.5.6" level="project" />
+    <orderEntry type="library" name="Maven: commons-codec:commons-codec:1.15" level="project" />
+  </component>
+</module>

+ 58 - 0
pom.xml

@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>AppInterface_jian</groupId>
+    <artifactId>AppInterface_jian</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <java.version>1.8</java.version>
+    </properties>
+    <parent>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-parent</artifactId>
+        <version>2.6.5</version>
+    </parent>
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.13</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpcore</artifactId>
+            <version>4.4.10</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.5.6</version>
+        </dependency>
+    </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.8.1</version>
+                <configuration>
+                    <failOnError>true</failOnError>
+                    <verbose>true</verbose>
+                    <fork>true</fork>
+                    <compilerArgument>-nowarn</compilerArgument>
+                    <source>${java.version}</source>
+                    <target>${java.version}</target>
+                    <encoding>UTF-8</encoding>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 19 - 0
src/main/java/com/zjj/SpringbootParent.java

@@ -0,0 +1,19 @@
+package com.zjj;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * @author wang_xy
+ * @description
+ * @Version 1.0
+ * @params
+ * @return
+ * @since 2022/9/13 13:24
+ */
+@SpringBootApplication
+public class SpringbootParent {
+    public static void main(String[] args) {
+        SpringApplication.run(SpringbootParent.class,args);
+    }
+}

+ 183 - 0
src/main/java/com/zjj/config/HttpUtil.java

@@ -0,0 +1,183 @@
+package com.zjj.config;
+
+import com.alibaba.fastjson.JSONObject;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.utils.URIBuilder;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.message.BasicHeader;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.util.EntityUtils;
+
+import java.io.*;
+import java.net.URI;
+import java.util.*;
+
+/**
+ * http请求工具
+ */
+public class HttpUtil {
+
+    /**
+     * 发送post请求携带json数据
+     * @param URL
+     * @param json
+     * @param headerMap
+     * @return
+     */
+    public static String sendPostJson(String URL, JSONObject json, HashMap<String,String> headerMap) {
+        CloseableHttpClient client = HttpClients.createDefault();
+        HttpPost post = new HttpPost(URL);
+        Iterator<Map.Entry<String, String>> iterator = headerMap.entrySet().iterator();
+        while (iterator.hasNext()) {
+            Map.Entry<String, String> next = iterator.next();
+            post.addHeader(next.getKey(),next.getValue());
+        }
+        String result;
+        try {
+            StringEntity s = new StringEntity(json.toString(), "utf-8");
+            s.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
+                    "application/json"));
+            post.setEntity(s);
+            // 发送请求
+            HttpResponse httpResponse = client.execute(post);
+            System.out.println(httpResponse.getEntity().toString());
+            // 获取响应输入流
+            InputStream inStream = httpResponse.getEntity().getContent();
+            BufferedReader reader = new BufferedReader(new InputStreamReader(
+                    inStream, "utf-8"));
+            StringBuilder strber = new StringBuilder();
+            String line;
+            while ((line = reader.readLine()) != null)
+                strber.append(line + "\n");
+            inStream.close();
+            result = strber.toString();
+            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+                System.out.println("请求服务器成功,做相应处理");
+            } else {
+                System.out.println("请求服务端失败");
+            }
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        return result;
+    }
+
+    /**
+     * @param url 访问地址
+     * @param headerMap  header 参数;可以通过下面工具类将string类型转换成map
+     * @param contentMap 需要传输参数参数;对象可以通过json转换成map
+     * @return 返回网页返回的数据
+     */
+    public static String sendPostForm(String url, Map<String, String> headerMap, Map<String, String> contentMap) {
+        String result = null;
+        CloseableHttpClient httpClient = HttpClients.createDefault();
+        HttpPost post = new HttpPost(url);
+
+        // 设置请求头
+//        post.addHeader("Content-Type", "application/x-www-form-urlencoded");
+
+        List<NameValuePair> content = new ArrayList<NameValuePair>();
+        //将content生成entity
+        Iterator iterator = contentMap.entrySet().iterator();
+        while (iterator.hasNext()) {
+            Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
+            content.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
+        }
+        CloseableHttpResponse response = null;
+        try {
+            //循环增加header
+            Iterator headerIterator = headerMap.entrySet().iterator();
+            while (headerIterator.hasNext()) {
+                Map.Entry<String, String> elem = (Map.Entry<String, String>) headerIterator.next();
+                post.addHeader(elem.getKey(), elem.getValue());
+            }
+            if (content.size() > 0) {
+                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(content, "UTF-8");
+                post.setEntity(entity);
+            }
+            //发送请求并接收返回数据
+            response = httpClient.execute(post);
+            if (response != null && response.getStatusLine().getStatusCode() == 200) {
+                //获取response的body部分
+                HttpEntity entity = response.getEntity();
+                //读取reponse的body部分并转化成字符串
+                result = EntityUtils.toString(entity);
+            }
+            return result;
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        } catch (ClientProtocolException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                httpClient.close();
+                if (response != null) {
+                    response.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return null;
+    }
+    /**
+     * 发送get请求
+     * @param url 请求URL
+     * @param param 请求参数 key:value url携带参数 或者无参可不填
+     * @return
+     */
+    public static String doGet(String url, Map<String, String> param) {
+
+        // 创建Httpclient对象
+        CloseableHttpClient httpclient = HttpClients.createDefault();
+
+        String resultString = "";
+        CloseableHttpResponse response = null;
+        try {
+            // 创建uri
+            URIBuilder builder = new URIBuilder(url);
+            if (param != null) {
+                for (String key : param.keySet()) {
+                    builder.addParameter(key, param.get(key));
+                }
+            }
+            URI uri = builder.build();
+
+            // 创建http GET请求
+            HttpGet httpGet = new HttpGet(uri);
+
+            // 执行请求
+            response = httpclient.execute(httpGet);
+            // 判断返回状态是否为200
+            if (response.getStatusLine().getStatusCode() == 200) {
+                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                if (response != null) {
+                    response.close();
+                }
+                httpclient.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return resultString;
+    }
+
+}

+ 15 - 0
src/main/java/com/zjj/config/URLConfig.java

@@ -0,0 +1,15 @@
+package com.zjj.config;
+
+/**
+ * @author wang_xy
+ * @description
+ * @Version 1.0
+ * @params
+ * @return
+ * @since 2022/9/13 13:56
+ */
+public class URLConfig {
+
+    public static final String url = "http://localhost:8882/a";
+    public static String LOGINMOBILE = url + "/loginMobile";
+}

+ 21 - 0
src/main/java/com/zjj/controller/AppInterfaceController.java

@@ -0,0 +1,21 @@
+package com.zjj.controller;
+
+import com.zjj.config.HttpUtil;
+import com.zjj.config.URLConfig;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@RestController
+public class AppInterfaceController {
+
+    @PostMapping("/loginMobile")
+    public String loginMobile(String username,String password){
+        Map param = new HashMap<>();
+        param.put("username",username);
+        param.put("password",password);
+        return HttpUtil.sendPostForm(URLConfig.LOGINMOBILE,new HashMap<>(),param);
+    }
+}

+ 7 - 0
src/main/resources/application.yml

@@ -0,0 +1,7 @@
+#访问端口
+server:
+  port: 8089
+  tomcat:
+    uri-encoding: UTF-8
+  servlet:
+    context-path: /