zhentao 1 月之前
當前提交
129d54d031

+ 33 - 0
.gitignore

@@ -0,0 +1,33 @@
+HELP.md
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/

+ 95 - 0
pom.xml

@@ -0,0 +1,95 @@
+<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.zhentao</groupId>
+    <artifactId>lianxi3-30</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <name>lianxi3-30</name>
+    <description>lianxi3-30</description>
+    <properties>
+        <java.version>1.8</java.version>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+        <spring-boot.version>2.6.13</spring-boot.version>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.mysql</groupId>
+            <artifactId>mysql-connector-j</artifactId>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.security</groupId>
+            <artifactId>spring-security-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-validation</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.16</version>
+        </dependency>
+    </dependencies>
+    <dependencyManagement>
+        <dependencies>
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-dependencies</artifactId>
+                <version>${spring-boot.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.8.1</version>
+                <configuration>
+                    <source>1.8</source>
+                    <target>1.8</target>
+                    <encoding>UTF-8</encoding>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>${spring-boot.version}</version>
+                <configuration>
+                    <mainClass>com.zhentao.lianxi330.Lianxi330Application</mainClass>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>repackage</id>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 13 - 0
src/main/java/com/zhentao/lianxi330/Lianxi330Application.java

@@ -0,0 +1,13 @@
+package com.zhentao.lianxi330;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Lianxi330Application {
+
+    public static void main(String[] args) {
+        SpringApplication.run(Lianxi330Application.class, args);
+    }
+
+}

+ 43 - 0
src/main/java/com/zhentao/lianxi330/controller/ApiController.java

@@ -0,0 +1,43 @@
+package com.zhentao.lianxi330.controller;
+
+import com.zhentao.lianxi330.model.RequestParams;
+import com.zhentao.lianxi330.service.ApiService;
+import org.springframework.http.ResponseEntity;
+import org.springframework.validation.BindingResult;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Map;
+import java.util.UUID;
+
+@RestController
+@RequestMapping("/api")
+
+public class ApiController {
+
+    private final ApiService apiService;
+
+    public ApiController(ApiService apiService) {
+        this.apiService = apiService;
+    }
+
+    @PostMapping("/post")
+    public ResponseEntity<Map<String, Object>> postRequest(@RequestHeader(value = "reqId", defaultValue = "") String reqId,
+                                                           @Validated @RequestBody RequestParams params,
+                                                           BindingResult bindingResult) {
+        if (reqId.isEmpty()) {
+            reqId = UUID.randomUUID().toString();
+        }
+
+        return apiService.handlePostRequest(reqId, params, bindingResult);
+    }
+
+    @GetMapping("/get")
+    public ResponseEntity<Map<String, Object>> getRequest(@RequestHeader(value = "reqId", defaultValue = "") String reqId,@Validated RequestParams params,
+                                                          BindingResult bindingResult) {
+        if (reqId.isEmpty()) {
+            reqId = UUID.randomUUID().toString();
+        }
+        return apiService.handleGetRequest(reqId, params, bindingResult);
+    }
+}

+ 44 - 0
src/main/java/com/zhentao/lianxi330/model/RequestParams.java

@@ -0,0 +1,44 @@
+package com.zhentao.lianxi330.model;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+import org.springframework.format.annotation.DateTimeFormat;
+
+import javax.validation.constraints.*;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.Date;
+
+@Data
+public class RequestParams {
+
+    @NotBlank(message = "appId 不能为空")
+    private String appId;
+
+    @NotBlank(message = "sign 不能为空")
+    private String sign;
+
+    @NotNull(message = "reqTime 不能为空")
+    private Long reqTime;
+
+    @NotBlank(message = "goodsId 不能为空")
+    private String goodsId;
+
+    @NotNull(message = "amount 不能为空")
+    @Min(value = 1, message = "amount 必须大于等于 1")
+    private Integer amount;
+
+    @NotNull(message = "price 不能为空")
+    @Min(value = 0, message = "price 必须大于等于 0")
+    @Max(value = 9999, message = "price 必须小于等于 9999")
+    private Double price;
+
+    @NotBlank(message = "mobile 不能为空")
+    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号码格式不正确")
+    private String mobile;
+
+    @NotNull(message = "nowDate 不能为空")
+    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private String nowDate;
+}

+ 12 - 0
src/main/java/com/zhentao/lianxi330/service/ApiService.java

@@ -0,0 +1,12 @@
+package com.zhentao.lianxi330.service;
+
+import com.zhentao.lianxi330.model.RequestParams;
+import org.springframework.http.ResponseEntity;
+import org.springframework.validation.BindingResult;
+
+import java.util.Map;
+
+public interface ApiService {
+    ResponseEntity<Map<String, Object>> handlePostRequest(String reqId, RequestParams params, BindingResult bindingResult);
+    ResponseEntity<Map<String, Object>> handleGetRequest(String reqId, RequestParams params, BindingResult bindingResult);
+}

+ 43 - 0
src/main/java/com/zhentao/lianxi330/service/impl/ApiServiceImpl.java

@@ -0,0 +1,43 @@
+package com.zhentao.lianxi330.service.impl;
+
+import com.zhentao.lianxi330.model.RequestParams;
+import com.zhentao.lianxi330.service.ApiService;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.validation.BindingResult;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Service
+public class ApiServiceImpl implements ApiService {
+
+    @Override
+    public ResponseEntity<Map<String, Object>> handlePostRequest(String reqId, RequestParams params, BindingResult bindingResult) {
+        Map<String, Object> response = new HashMap<>();
+        response.put("reqId", reqId);
+        if (bindingResult.hasErrors()) {
+            response.put("message", "参数验证失败");
+            response.put("errors", bindingResult.getAllErrors());
+            return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
+        }
+        response.put("message", "请求成功");
+        response.put("data", params);
+        return new ResponseEntity<>(response, HttpStatus.OK);
+    }
+
+    @Override
+    public ResponseEntity<Map<String, Object>> handleGetRequest(String reqId, RequestParams params, BindingResult bindingResult) {
+        Map<String, Object> response = new HashMap<>();
+        response.put("reqId", reqId);
+        if (bindingResult.hasErrors()) {
+            response.put("message", "参数验证失败");
+            response.put("errors", bindingResult.getAllErrors());
+            return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
+        }
+        response.put("message", "请求成功");
+        response.put("data", params);
+        return new ResponseEntity<>(response, HttpStatus.OK);
+    }
+}

+ 4 - 0
src/main/resources/application.properties

@@ -0,0 +1,4 @@
+server.port=8080
+
+
+

+ 6 - 0
src/main/resources/static/index.html

@@ -0,0 +1,6 @@
+<html>
+<body>
+<h1>hello word!!!</h1>
+<p>this is a html page</p>
+</body>
+</html>

+ 94 - 0
src/test/java/com/zhentao/lianxi330/ApiTest.java

@@ -0,0 +1,94 @@
+package com.zhentao.lianxi330;
+
+import cn.hutool.crypto.digest.DigestUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONUtil;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+public class ApiTest {
+    private static final String BASE_URL = "http://localhost:8080/api";
+    private static final String APP_KEY = "your_app_key";
+    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
+
+    public static void main(String[] args) {
+        postRequest();
+        getRequest();
+    }
+
+    private static void postRequest() {
+        String reqId = UUID.randomUUID().toString();
+        String appId = "123456";
+        String goodsId = "G001";
+        long reqTime = System.currentTimeMillis();
+        LocalDateTime nowDate = LocalDateTime.now();
+        String nowDateStr = nowDate.format(FORMATTER);
+        int amount = 2;
+        double price = 99.9;
+        String mobile = "13800138000";
+
+        String sign = generateSign(appId, goodsId, reqId, reqTime, APP_KEY);
+
+        Map<String, Object> params = new HashMap<>();
+        params.put("appId", appId);
+        params.put("sign", sign);
+        params.put("reqTime", reqTime);
+        params.put("goodsId", goodsId);
+        params.put("amount", amount);
+        params.put("price", price);
+        params.put("mobile", mobile);
+        params.put("nowDate", nowDateStr);
+
+        HttpResponse response = HttpRequest.post(BASE_URL + "/post")
+                .header("reqId", reqId)
+                .header("Content-Type", "application/json")
+                .body(JSONUtil.toJsonStr(params))
+                .execute();
+
+        System.out.println("POST Response Status: " + response.getStatus());
+        System.out.println("POST Response Body: " + response.body());
+    }
+
+    private static void getRequest() {
+        String reqId = UUID.randomUUID().toString();
+        String appId = "123456";
+        String goodsId = "G001";
+        long reqTime = System.currentTimeMillis();
+        LocalDateTime nowDate = LocalDateTime.now();
+        String nowDateStr = nowDate.format(FORMATTER);
+        int amount = 2;
+        double price = 99.9;
+        String mobile = "13800138000";
+
+        String sign = generateSign(appId, goodsId, reqId, reqTime, APP_KEY);
+
+        Map<String, Object> params = new HashMap<>();
+        params.put("appId", appId);
+        params.put("sign", sign);
+        params.put("reqTime", reqTime);
+        params.put("goodsId", goodsId);
+        params.put("amount", amount);
+        params.put("price", price);
+        params.put("mobile", mobile);
+        params.put("nowDate", nowDateStr);
+
+        String queryString = HttpUtil.toParams(params);
+        HttpResponse response = HttpRequest.get(BASE_URL + "/get?" + queryString)
+                .header("reqId", reqId)
+                .execute();
+
+        System.out.println("GET Response Status: " + response.getStatus());
+        System.out.println("GET Response Body: " + response.body());
+    }
+
+    private static String generateSign(String appId, String goodsId, String reqId, long reqTime, String appKey) {
+        String source = appId + goodsId + reqId + reqTime + appKey;
+        return DigestUtil.md5Hex(source);
+    }
+}

+ 13 - 0
src/test/java/com/zhentao/lianxi330/Lianxi330ApplicationTests.java

@@ -0,0 +1,13 @@
+package com.zhentao.lianxi330;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class Lianxi330ApplicationTests {
+
+    @Test
+    void contextLoads() {
+    }
+
+}