频繁使用且并非特别重要的数据 用redis去简单储存:
例如:店铺营业状态 status 1是营业中 0是打样中
pom中 引入使用SpirngData操作redis的依赖 ,使用特别方便,yml文件中配置 :
redis: host: ${sky.redis.host} port: ${sky.redis.port} database: ${sky.redis.database}
然后创建一个config配置一下redis的序列化和反序列化,默认使用的jdk的序列化,然后简单使用redistemplate操作一下字符串的操作,set,get方法存取。
@Autowired private RedisTemplate redisTemplate; @PutMapping("/{status}") @ApiOperation("设置店铺营业状态") public Result setStatus(@PathVariable Integer status){ log.info("设置店铺营业状态{}",status == 1?"营业中": "打烊中"); redisTemplate.opsForValue().set("SHOP_STATUS",status); return Result.success(); } @GetMapping("/status") @ApiOperation("获取店铺的营业状态") public Result<Integer> getStatus(){ Integer status = (Integer) redisTemplate.opsForValue().get("SHOP_STATUS"); log.info("获取店铺的营业状态为:{}",status ==1?"营业中":"打样中"); return Result.success(status); }
然后就看到一个HTTP协议的客户端编程工具包,还是比较简单的api:
具体简单实现 http的get and post方法
get方法:
CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("访问路径"); CloseableHttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode();//状态码 HttpEntity entity = response.getEntity(); String body = EntityUtils.toString(entity);//数据 response.close(); httpClient.close();
post方法:
CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(""); JSONObject jsonObject = new JSONObject(); jsonObject.put("username","admin"); jsonObject.put("username1","admin1"); StringEntity stringEntity = new StringEntity(jsonObject.toString()); stringEntity.setContentType("utf-8"); stringEntity.setContentType("application/json"); httpPost.setEntity(stringEntity); CloseableHttpResponse response = httpClient.execute(httpPost);