Spring Boot (2)

tech2024-07-27  55

继续上期 添加一个config文件夹及Swagger2Config文件 在其中写入

package com.nmgdz.bigdata.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * Swagger2API文档的配置 */ @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包下controller生成API文档 .apis(RequestHandlerSelectors.basePackage("com.nmgdz.bigdata.controller")) //为有@Api注解的Controller生成API文档 // .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) //为有@ApiOperation注解的方法生成API文档 // .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SwaggerUI演示") .description("内蒙古电子大数据工坊") .contact("xhd") .version("1.0") .build(); } }

修改UserController文件

package com.nmgdz.bigdata.controller; import com.nmgdz.bigdata.entity.User; import com.nmgdz.bigdata.service.RedisService; import com.nmgdz.bigdata.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("user") public class UserController { @Autowired private UserService userService; @Autowired private RedisService redisService; @GetMapping("/gueryBuId") public String gueryById(int id) { String userStr = redisService.get(id + ""); if (userStr==null) { redisService.set(id + "", userService.gueryById(id).toString()); } return userStr; } /** * 增 改 * @param user * @return */ @PostMapping("/saveOrUpdate") public User saveOrUpdateUser(User user) { return userService.save(user); } /** * 删除用户 * @param id * @return */ @DeleteMapping("/delete") public Boolean deleteUser(int id) { userService.deleUserById(id); return true; } }

添加RedisTest文件

package com.nmgdz.bigdata; import com.nmgdz.bigdata.service.RedisService; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @Slf4j @SpringBootTest public class RedisTest { @Autowired private RedisService redisService; //TODO main函数测试 @Test public void testRedis(){ redisService.set("dianzi","123456"); System.out.println("设置得值为---"+redisService.get("dianzi")); } }

查看函数测试 在网页上查看http://localhost:8080/swagger-ui.html

最新回复(0)