zhentao 1 개월 전
커밋
cb173fd252

+ 33 - 0
demo02/.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
demo02/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>demo02</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <name>demo02</name>
+    <description>demo02</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>com.mysql</groupId>
+            <artifactId>mysql-connector-j</artifactId>
+            <scope>runtime</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-validation</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</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.ApiApplication</mainClass>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>repackage</id>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>

+ 11 - 0
demo02/src/main/java/com/zhentao/ApiApplication.java

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

+ 57 - 0
demo02/src/main/java/com/zhentao/controller/ApiController.java

@@ -0,0 +1,57 @@
+package com.zhentao.controller;
+
+import com.zhentao.pojo.Api;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.validation.BindingResult;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+
+import javax.validation.Valid;
+import java.util.HashMap;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/api")
+@Validated   // @Validated 注解用于开启方法级别的参数验证
+public class ApiController {
+
+
+     // @RequestHeader 注解用于获取请求头中的 reqId 参数
+     // @Valid 注解用于对请求体中的 ApiRequest 对象进行验证
+     // BindingResult 用于存储验证结果
+    @PostMapping("/post")
+    public ResponseEntity<Map<String, Object>> postApi(@RequestHeader("reqId") String reqId,
+                                                       @Valid @RequestBody Api pojo,
+                                                       BindingResult bindingResult) {
+
+        Map<String, Object> response = new HashMap<>();
+        if (bindingResult.hasErrors()) {
+            //请求失败
+            response.put("success", false);
+            //获取第一个错误信息
+            response.put("message", bindingResult.getAllErrors().get(0).getDefaultMessage());
+
+            return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
+        }
+        //验证通过,成功
+        response.put("success", true);
+        response.put("message", "请求成功");
+        return new ResponseEntity<>(response, HttpStatus.OK);
+    }
+
+    @GetMapping("/get")
+    public ResponseEntity<Map<String, Object>> getApi(@RequestHeader("reqId") String reqId,
+                                                      @Valid Api pojo,
+                                                      BindingResult bindingResult) {
+        Map<String, Object> response = new HashMap<>();
+        if (bindingResult.hasErrors()) {
+            response.put("success", false);
+            response.put("message", bindingResult.getAllErrors().get(0).getDefaultMessage());
+            return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
+        }
+        response.put("success", true);
+        response.put("message", "请求成功");
+        return new ResponseEntity<>(response, HttpStatus.OK);
+    }
+}

+ 18 - 0
demo02/src/main/java/com/zhentao/controller/InterService.java

@@ -0,0 +1,18 @@
+package com.zhentao.controller;
+
+import cn.hutool.crypto.digest.DigestUtil;
+import org.springframework.util.DigestUtils;
+
+public class InterService {
+    public static boolean verifySignature1(String appId, String goodsId, String reqId, String reqTime, String appKey, String signature) {
+        String data = appId + goodsId + reqId + reqTime + appKey;
+        String md5Str = DigestUtil.md5Hex(data);
+        return md5Str.equals(signature);
+    }
+
+    public static boolean verifySignature2(String appId, String goodsId, String reqId, String reqTime, String appKey, String signature) {
+        String data = appId + goodsId + reqId + reqTime + appKey;
+        String md5Str = DigestUtil.md5Hex(data);
+        return md5Str.equals(signature);
+    }
+}

+ 23 - 0
demo02/src/main/java/com/zhentao/controller/MinNumSubString.java

@@ -0,0 +1,23 @@
+package com.zhentao.controller;
+
+public class MinNumSubString {
+    public static String minWindow(String s,String t){
+        if(s ==null || t==null || s.length() == 0 || t.length() ==0 || s.length()<t.length()){
+            return "";
+        }
+        int[] target=new int[128];
+        for (char c: t.toCharArray()) {
+            target[c]++;
+        }
+        int left=0, right =0;
+        int minLeft =0, minLen=s.length()+1;
+        int count=t.length();
+        while (right<s.length()){
+            char c=s.charAt(right);
+            if(target[c] >0){
+                count--;
+            }
+        }
+        return null;
+    }
+}

+ 39 - 0
demo02/src/main/java/com/zhentao/pojo/Api.java

@@ -0,0 +1,39 @@
+package com.zhentao.pojo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import lombok.Data;
+
+import javax.validation.constraints.*;
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+@Data
+public class Api implements Serializable {
+    @NotBlank(message = "应用不能为空")
+    private String appId;
+    @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;
+    @NotNull(message = "amount 不能为空")
+    @DecimalMin(value ="1",message = "最小值1")
+    private Integer amount;
+    @NotNull(message = "price 不能为空")
+    @DecimalMin(value ="0",message = "最小值为0")
+    @DecimalMax(value = "9999",message = "最大值9999")
+    private BigDecimal price;
+    @NotBlank(message = "手机号不能为空")
+    @Pattern(regexp="^((13[0-9)|(14[0-9])|(15(10-9])|(166)|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$",message ="手机号码格式错误")
+    private String mobile;
+    @NotBlank(message = "日期不能为空")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8",shape = JsonFormat.Shape.STRING)
+    private String nowDate;
+
+
+}

+ 13 - 0
demo02/src/main/java/com/zhentao/servicve/DemoService.java

@@ -0,0 +1,13 @@
+package com.zhentao.servicve;
+
+import com.zhentao.pojo.Api;
+import org.springframework.http.ResponseEntity;
+
+import java.util.Map;
+
+public interface DemoService {
+
+
+
+
+}

+ 14 - 0
demo02/src/main/java/com/zhentao/servicve/impl/DemoServiceimpl.java

@@ -0,0 +1,14 @@
+package com.zhentao.servicve.impl;
+
+import com.zhentao.pojo.Api;
+import com.zhentao.servicve.DemoService;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.Map;
+@Service
+public class DemoServiceimpl implements DemoService {
+
+}

+ 8 - 0
demo02/src/main/resources/application.properties

@@ -0,0 +1,8 @@
+#下面这些内容是为了让MyBatis映射
+#指定Mybatis的Mapper文件
+mybatis.mapper-locations=classpath:mappers/*xml
+#指定Mybatis的实体目录
+mybatis.type-aliases-package=com.zhentao.demo02.mybatis.entity
+# 应用服务 WEB 访问端口
+server.port=8080
+

+ 6 - 0
demo02/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>

+ 13 - 0
demo02/src/test/java/com/zhentao/demo02/Demo02ApplicationTests.java

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