Просмотр исходного кода

http://git.workervip.com/lishilong-2407/zy.git

李世龙 1 месяц назад
Сommit
d3eb12cf79

+ 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/

+ 86 - 0
pom.xml

@@ -0,0 +1,86 @@
+<?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.zt</groupId>
+    <artifactId>demo1</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <name>demo1</name>
+    <description>demo1</description>
+    <properties>
+        <java.version>17</java.version>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+        <spring-boot.version>3.0.2</spring-boot.version>
+    </properties>
+    <dependencies>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <!-- Spring Validation 依赖,用于参数验证 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-validation</artifactId>
+        </dependency>
+        <!-- Spring Web 依赖,用于构建 RESTful 接口 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </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>17</source>
+                    <target>17</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.zt.demo.Demo1Application</mainClass>
+                    <skip>true</skip>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>repackage</id>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 13 - 0
src/main/java/com/zt/demo/Demo1Application.java

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

+ 34 - 0
src/main/java/com/zt/demo/controller/ApiController.java

@@ -0,0 +1,34 @@
+package com.zt.demo.controller;
+
+import com.zt.demo.pojo.RequestParams;
+import com.zt.demo.service.RequestParamsService;
+import jakarta.validation.Valid;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+
+@RestController
+@RequestMapping("/api")
+@Slf4j
+@Validated
+public class ApiController {
+    @Autowired
+    private RequestParamsService requestParamsService;
+
+    @PostMapping("/post")
+    public ResponseEntity<String> postRequest(@RequestHeader(value = "reqId", required = false) String reqId, @Valid @RequestBody RequestParams params) {
+        String result = requestParamsService.handlePostRequest(params);
+        log.info(result.toString());
+        return new ResponseEntity<>(result, HttpStatus.OK);
+    }
+
+    @GetMapping("/get")
+    public ResponseEntity<String> getRequest(@RequestHeader(value = "reqId", required = false) String reqId, @Valid RequestParams params) {
+        String result = requestParamsService.handleGetRequest(params);
+        log.info(result.toString());
+        return new ResponseEntity<>(result, HttpStatus.OK);
+    }
+}

+ 36 - 0
src/main/java/com/zt/demo/pojo/RequestParams.java

@@ -0,0 +1,36 @@
+package com.zt.demo.pojo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import jakarta.validation.constraints.*;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+@Data
+public class RequestParams implements Serializable {
+    @NotBlank(message = "appId不能为空")
+    private String appId;//应用id
+    @NotBlank(message ="签名不能为空")
+    private String sign;//签名
+    @NotNull(message ="时间戳不能为空")
+    @JsonSerialize(using = ToStringSerializer.class)
+    private Long reqTime;//时间戳
+    @NotNull(message ="goodsId不能为空")
+    @JsonFormat(shape = JsonFormat.Shape.STRING)
+    private Long goodsId;//商品id
+    @NotNull(message ="数量不能为空")
+    @DecimalMin(value = "1",message = "最小值1")
+    private Integer amount;//数量
+    @DecimalMin(value="0",message = "最小值")
+    @DecimalMax(value="9999",message = "最大值")
+    private BigDecimal price;//价格
+    @NotBlank(message ="手机号不能为空")
+    @Pattern(regexp = "^((13[0-9])|(14[0-9])|(15[0-9])|(166)|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$",message = "手机号格式错误")
+    private String mobile;//手机号
+    @NotNull(message ="sign 不能为空")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8",shape = JsonFormat.Shape.STRING)
+    private Date nowDate;//当前时间
+}

+ 8 - 0
src/main/java/com/zt/demo/service/RequestParamsService.java

@@ -0,0 +1,8 @@
+package com.zt.demo.service;
+
+import com.zt.demo.pojo.RequestParams;
+
+public interface RequestParamsService {
+    String handlePostRequest(RequestParams params);
+    String handleGetRequest(RequestParams params);
+}

+ 20 - 0
src/main/java/com/zt/demo/service/impl/RequestParamsServiceImpl.java

@@ -0,0 +1,20 @@
+package com.zt.demo.service.impl;
+
+import com.zt.demo.pojo.RequestParams;
+import com.zt.demo.service.RequestParamsService;
+import org.springframework.stereotype.Service;
+
+@Service
+public class RequestParamsServiceImpl implements RequestParamsService {
+    @Override
+    public String handlePostRequest(RequestParams params) {
+        // 这里可以添加具体的业务逻辑
+        return "POST 请求处理成功";
+    }
+
+    @Override
+    public String handleGetRequest(RequestParams params) {
+        // 这里可以添加具体的业务逻辑
+        return "GET 请求处理成功";
+    }
+}

+ 13 - 0
src/test/java/com/zt/demo/Demo1ApplicationTests.java

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