唐tag 1 년 전
부모
커밋
5f6ad34177

+ 18 - 0
pom.xml

@@ -425,6 +425,24 @@
             <version>2.0.39</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.github.xiaoymin</groupId>
+            <artifactId>knife4j-micro-spring-boot-starter</artifactId>
+            <version>3.0.2</version>
+        </dependency>
+        <dependency>
+            <groupId>com.github.xiaoymin</groupId>
+            <!--使用Swagger2-->
+            <artifactId>knife4j-spring-boot-starter</artifactId>
+            <version>3.0.2</version>
+        </dependency>
+
+        <dependency>
+            <groupId>commons-beanutils</groupId>
+            <artifactId>commons-beanutils</artifactId>
+            <version>1.9.4</version>
+        </dependency>
+
     </dependencies>
 
 

+ 53 - 1
src/main/java/com/zswl/dataservicestarter/config/SwaggerConfig.java

@@ -1,11 +1,63 @@
 package com.zswl.dataservicestarter.config;
 
 import com.mongodb.MongoClientSettings;
+import io.swagger.annotations.Api;
+import lombok.extern.java.Log;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
+import org.springframework.util.StringUtils;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import springfox.documentation.builders.ApiInfoBuilder;
+import springfox.documentation.builders.PathSelectors;
+import springfox.documentation.builders.RequestHandlerSelectors;
+import springfox.documentation.oas.annotations.EnableOpenApi;
+import springfox.documentation.service.ApiInfo;
+import springfox.documentation.service.Contact;
+import springfox.documentation.spi.DocumentationType;
+import springfox.documentation.spring.web.plugins.Docket;
+import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Vector;
+import java.util.stream.Collectors;
 
 /**
  *
  */
-//@Configuration
+@Log
+@EnableOpenApi
+@Configuration
+@EnableSwagger2WebMvc
 public class SwaggerConfig {
+
+    @Value("${spring.application.name}")
+    private String applicationName;
+
+    @Bean
+    public Docket defaultApi() {
+        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName(applicationName).select()
+                // 添加@Api注解才显示
+                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
+                // 这里指定Controller扫描包路径
+                .paths(PathSelectors.any()).build();
+    }
+
+    /**
+     * swagger-api接口描述信息
+     */
+    private ApiInfo apiInfo() {
+        return new ApiInfoBuilder().title("API文档")
+                .description("中数未来数据服务")
+                .contact(new Contact("RTX", "", ""))
+                .version("1.0.0").build();
+    }
 }

+ 5 - 1
src/main/java/com/zswl/dataservicestarter/config/WebMvcConfig.java

@@ -15,5 +15,9 @@ import java.util.List;
  */
 @Configuration
 public class WebMvcConfig implements WebMvcConfigurer {
-
+    @Override
+    public void addResourceHandlers(ResourceHandlerRegistry registry) {
+        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
+        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
+    }
 }

+ 4 - 0
src/main/java/com/zswl/dataservicestarter/controller/OauthController.java

@@ -4,6 +4,9 @@ import com.zswl.dataservicestarter.model.TokenModel;
 import com.zswl.dataservicestarter.model.params.TokenParam;
 import com.zswl.dataservicestarter.service.AppInfoService;
 import com.zswl.dataservicestarter.utils.result.ResultContent;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -31,6 +34,7 @@ public class OauthController {
      * @param param
      * @return
      */
+    @ApiOperation(value = "得到token", notes = "得到token")
     @RequestMapping(value = "token", method = {RequestMethod.POST})
     public ResultContent<TokenModel> token(@RequestBody TokenParam param) {
         return appInfoService.getToken(param);

+ 26 - 20
src/main/java/com/zswl/dataservicestarter/controller/TestController.java

@@ -3,6 +3,8 @@ package com.zswl.dataservicestarter.controller;
 import com.github.javaparser.quality.NotNull;
 import com.zswl.dataservicestarter.service.UserService;
 import com.zswl.dataservicestarter.utils.result.ResultContent;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
 import lombok.Cleanup;
 import lombok.SneakyThrows;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -17,32 +19,36 @@ import java.io.InputStream;
 import java.util.HashMap;
 import java.util.Map;
 
-/** */
+/**
+ *
+ */
 @RequestMapping("/test")
 @RestController
 @Validated
 public class TestController {
 
-  @Autowired UserService userService;
+    @Autowired
+    UserService userService;
 
-  @RequestMapping(
-      value = "test",
-      method = {RequestMethod.GET, RequestMethod.POST})
-  public ResultContent test(String name) {
-    Map map = new HashMap();
-    map.put("obj", name);
-    map.put("time", System.currentTimeMillis());
+    @ApiOperation(value = "测试方法", notes = "测试方法")
+    @RequestMapping(
+            value = "test",
+            method = {RequestMethod.GET, RequestMethod.POST})
+    public ResultContent test(String name) {
+        Map map = new HashMap();
+        map.put("obj", name);
+        map.put("time", System.currentTimeMillis());
 
-    return userService.addUser(name);
-  }
+        return userService.addUser(name);
+    }
 
-  @RequestMapping(value = "uploadFileDelegate", method = RequestMethod.POST)
-  @SneakyThrows
-  public ResultContent uploadFileDelegate(
-      @RequestParam(value = "file") @NotNull MultipartFile multipartFile,
-      String path,
-      String epId) {
-    @Cleanup InputStream inputStream = multipartFile.getInputStream();
-    return ResultContent.buildSuccess();
-  }
+    @RequestMapping(value = "uploadFileDelegate", method = RequestMethod.POST)
+    @SneakyThrows
+    public ResultContent uploadFileDelegate(
+            @RequestParam(value = "file") @NotNull MultipartFile multipartFile,
+            String path,
+            String epId) {
+        @Cleanup InputStream inputStream = multipartFile.getInputStream();
+        return ResultContent.buildSuccess();
+    }
 }

+ 16 - 0
src/main/java/com/zswl/dataservicestarter/model/params/IDParam.java

@@ -0,0 +1,16 @@
+package com.zswl.dataservicestarter.model.params;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * @author TRX
+ * @date 2024/3/25
+ */
+@Data
+@ApiModel("ID模型")
+public class IDParam {
+    @ApiModelProperty("数据id")
+    private String id;
+}

+ 4 - 0
src/main/java/com/zswl/dataservicestarter/model/params/TokenParam.java

@@ -1,5 +1,7 @@
 package com.zswl.dataservicestarter.model.params;
 
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
@@ -11,10 +13,12 @@ import lombok.NoArgsConstructor;
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
+@ApiModel("得到token参数")
 public class TokenParam {
     /**
      * 渠道ID
      */
+    @ApiModelProperty(value = "渠道ID", required = true)
     private String entId;
     /**
      * appSecret

+ 19 - 16
src/main/java/com/zswl/dataservicestarter/service/UserService.java

@@ -18,24 +18,27 @@ import org.springframework.stereotype.Service;
 @Service
 public class UserService {
 
-  @Autowired UserDao userDao;
+    @Autowired
+    UserDao userDao;
 
-  @Autowired RedisService redisService;
+    @Autowired
+    RedisService redisService;
 
-  @Autowired GridFsTemplate gridFsTemplate;
+    @Autowired
+    GridFsTemplate gridFsTemplate;
 
-  public ResultContent addUser(String name) {
-    User user = new User();
-    if (StringUtils.isEmpty(name)) {
-      name = "名称";
+    public ResultContent addUser(String name) {
+        User user = new User();
+        if (StringUtils.isEmpty(name)) {
+            name = "名称";
+        }
+        //        name = name + Math.random();
+        user.setName(name);
+        user.setAge(1);
+        user.setAddress("重庆市渝北区");
+        userDao.save(user);
+        int code = user.hashCode();
+        log.info("名称: {} code {} redis: {}", name, code, AppInfoUtil.generateRandomString());
+        return ResultContent.buildSuccess(name + ": " + code);
     }
-    //        name = name + Math.random();
-    user.setName(name);
-    user.setAge(1);
-    user.setAddress("重庆市渝北区");
-    userDao.save(user);
-    int code = user.hashCode();
-    log.info("名称: {} code {} redis: {}", name, code, AppInfoUtil.generateRandomString());
-    return ResultContent.buildSuccess(name + ": " + code);
-  }
 }

+ 10 - 0
src/main/java/com/zswl/dataservicestarter/utils/CommonUtil.java

@@ -0,0 +1,10 @@
+package com.zswl.dataservicestarter.utils;
+
+/**
+ * 工具类方法
+ *
+ * @author TRX
+ * @date 2024/3
+ */
+public class CommonUtil {
+}

+ 6 - 0
src/main/java/com/zswl/dataservicestarter/utils/result/ResultContent.java

@@ -2,6 +2,8 @@ package com.zswl.dataservicestarter.utils.result;
 
 import com.fasterxml.jackson.annotation.JsonInclude;
 import com.zswl.dataservicestarter.type.ResultState;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.*;
 
 import java.util.Optional;
@@ -17,21 +19,25 @@ import java.util.function.Consumer;
 @NoArgsConstructor
 @ToString(callSuper = true)
 @JsonInclude(JsonInclude.Include.NON_NULL)
+@ApiModel("API返回封装")
 public class ResultContent<T> {
 
     //内容
     @Getter
     @Setter
+    @ApiModelProperty("返回内容")
     private T content;
 
     //状态
     @Getter
     @Setter
+    @ApiModelProperty("状态")
     private ResultState state;
 
     //文本
     @Getter
     @Setter
+    @ApiModelProperty("信息")
     private String msg;
 
 

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

@@ -68,4 +68,9 @@ authsettings:
    EQIDAQAB"
   tokenHeaderName: accessToken
 
+knife4j:
+  # 开启增强配置
+  enable: true
+  # 是否开启生产环境屏蔽   true:关闭swagger,false:开启swagger
+  production: false