Knife4j
Knife4j是Java MVC框架集成Swagger生成Api文档的增强解决方案
使用方法:1.导入knife4j的maven坐标
2.在配置类中加入knife4j相关配置
3.设置静态资源映射,否则接口文档页面无法访问
1 2 3 4 5
| <dependency> <groupId>com.gitthub.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency>
|
在config包下的WebMvcConfiguration的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package com.sky.config; import com.sky.interceptor.JwtTokenAdminInterceptor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; 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;
@Configuration @Slf4j public class WebMvcConfiguration extends WebMvcConfigurationSupport {
@Autowired private JwtTokenAdminInterceptor jwtTokenAdminInterceptor;
protected void addInterceptors(InterceptorRegistry registry) { log.info("开始注册自定义拦截器..."); registry.addInterceptor(jwtTokenAdminInterceptor) .addPathPatterns("/admin/**") .excludePathPatterns("/admin/employee/login"); }
@Bean public Docket docket() { ApiInfo apiInfo = new ApiInfoBuilder() .title("苍穹外卖项目接口文档") .version("2.0") .description("苍穹外卖项目接口文档") .build(); Docket docket = new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo) .select() .apis(RequestHandlerSelectors.basePackage("com.sky.controller")) .paths(PathSelectors.any()) .build(); return docket; }
protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
|
Swagger常用注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| @Api @ApiOperation @ApiModel @ApiModelProperty
@ApiModel(description = "员工登录时传递的数据模型") public class EmployeeLoginDTO implements Serializable { @ApiModelProperty("用户名") private String username;
@ApiModelProperty("密码") private String password;
}
|
1 2 3
| Employee employee = new Employee(); BeanUtils.copyProperties(employeeDTO, employee);
|
Redis
pom文件引入依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
|
config包下创建 RedisConfiguration
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Configuration public class RedisConfiguration { @Bean public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { log.info("开始创建RedisTemplate对象..."); RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); return redisTemplate; } }
|
使用
1 2 3 4
| @Autowired private RedisTemplate redisTemplate; redisTemplate.opsForValue().set("SHOP_STATUS", status); Integer status = (Integer) redisTemplate.opsForValue().get("SHOP_STATUS");
|
HttpClient
1 2 3 4 5
| <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency>
|
发送请求步骤
1.创建 HttpClient对象
2.创建Http请求对象
3.调用HttpClient的execute方法发送请求
1 2 3 4 5 6 7 8
| CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("https://jin88.top/xianyuepro/random"); CloseableHttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); String body = EntityUtils.toString(entity); System.out.println(body+"body"); response.close(); httpClient.close();
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("https://jin88.top/xianyuepro/index/login"); JSONObject jsonObject =new JSONObject(); jsonObject.put("phone", "17356288857"); jsonObject.put("pwd", "123456");
StringEntity stringEntity = new StringEntity(jsonObject.toJSONString());
stringEntity.setContentType("application/json"); httpPost.setEntity(stringEntity);
CloseableHttpResponse execute = httpClient.execute(httpPost); HttpEntity entity = execute.getEntity(); String body = EntityUtils.toString(entity); System.out.println(body+"body"); execute.close(); httpClient.close();
|
Spring Cache
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>2.7.3</version> </dependency>
|
1 2 3 4 5 6 7 8
| @SpringBootApplication @EnableTransactionManagement @EnableCaching
@Cacheable(cacheNames = "setmealCache", key = "#categoryId") @CachePut @CacheEvict(cacheNames = "setmealCache",allEntries = true) @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
|
Spring Task
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @EnableScheduling
@Component @Slf4j public class MyTask { @Scheduled(cron = "0/5 * * * * ?") public void executeTask(){ log.info("定时任务开始执行:{}", new Date()); }
}
LocalDateTime time = LocalDateTime.now().plusMinutes(-15); LocalDateTime.now().plusMinutes(15);
|
WebScoket
websocket包下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| package com.sky.websocket; import org.springframework.stereotype.Component; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.util.Collection; import java.util.HashMap; import java.util.Map;
@Component @ServerEndpoint("/ws/{sid}") public class WebSocketServer { private static Map<String, Session> sessionMap = new HashMap();
@OnOpen public void onOpen(Session session, @PathParam("sid") String sid) { System.out.println("客户端:" + sid + "建立连接"); sessionMap.put(sid, session); }
@OnMessage public void onMessage(String message, @PathParam("sid") String sid) { System.out.println("收到来自客户端:" + sid + "的信息:" + message); } @OnClose public void onClose(@PathParam("sid") String sid) { System.out.println("连接断开:" + sid); sessionMap.remove(sid); }
public void sendToAllClient(String message) { Collection<Session> sessions = sessionMap.values(); for (Session session : sessions) { try { session.getBasicRemote().sendText(message); } catch (Exception e) { e.printStackTrace(); } } }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.sky.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration public class WebSocketConfiguration {
@Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); }
}
|