当前位置: 首页 > news >正文

做网站还要数据库吗近期国内新闻

做网站还要数据库吗,近期国内新闻,wordpress情侣博客模板下载,做系统用什么网站文章目录 1、确认下单:购物车页面点击去结算1.1、在OrderController类中创建 trade 方法1.2、在CartController类中创建 checkedCartInfos1.3、CartServiceImpl 实现 checkedCartInfos的业务功能1.4、在service-cart-client模块下定义远程openFeign接口1.5、在SpzxO…

文章目录

  • 1、确认下单:购物车页面点击去结算
    • 1.1、在OrderController类中创建 trade 方法
    • 1.2、在CartController类中创建 checkedCartInfos
    • 1.3、CartServiceImpl 实现 checkedCartInfos的业务功能
    • 1.4、在service-cart-client模块下定义远程openFeign接口
    • 1.5、在SpzxOrderApplication类上加上@EnableFeignClients
    • 1.6、OrderServiceImpl 实现 trade的业务逻辑
    • 1.7、此时启动 SpzxOrderApplication
  • 2、 openFeign拦截器使用
    • 2.1、使用feign拦截器拦截请求,获取token,重新传递token
      • 2.1.1、CartClientInterceptor
      • 2.1.2、@EnableCartClientConfig
      • 2.1.3、SpzxOrderApplication

1、确认下单:购物车页面点击去结算

  • 点击去结算这个按钮,会发起一个请求,这个请求是trade,然后展示我们要购买的商品商品的总金额
    在这里插入图片描述

1.1、在OrderController类中创建 trade 方法

@RestController
@Tag(name = "订单管理模块", description = "订单管理模块")
@RequestMapping("/api/order/orderInfo")
public class OrderController {@Resourceprivate OrderService orderService;//查询购物车中选中的购物项列表 转为 orderItem 列表交给前端展示@Operation(summary = "确认下单:购物车页面点击去结算")@GetMapping("/auth/trade")public Result trade() {TradeVo tradeVo = orderService.trade();return Result.ok(tradeVo);}
}

1.2、在CartController类中创建 checkedCartInfos

@RestController
@RequestMapping("/api/order/cart")
@Tag(name = "购物车模块")
public class CartController {//只要请求头中携带token,不需要再传用户id@Operation(summary = "查询用户购物车已选中购物项列表")@GetMapping("/auth/checkedCartInfos")public Result checkedCartInfos(){List<CartInfo> cartInfos = cartService.checkedCartInfos();return Result.ok(cartInfos);}
}

1.3、CartServiceImpl 实现 checkedCartInfos的业务功能

@Service
public class CartServiceImpl implements CartService {@Resourceprivate RedisTemplate redisTemplate;private BoundHashOperations getUserCart() {UserInfo userInfo = SpzxServiceAuthInterceptor.THREAD_LOCAL.get();BoundHashOperations ops = redisTemplate.boundHashOps("spzx:cart:" + userInfo.getId());return ops;}@Overridepublic List<CartInfo> checkedCartInfos() {//泛型1:redis键类型,泛型2:hash的key类型, 泛型3:hash的value的类型BoundHashOperations<String,String,CartInfo> userCart = getUserCart();return userCart.values().stream().filter(cartInfo -> cartInfo.getIsChecked()==1).toList();}
}

1.4、在service-cart-client模块下定义远程openFeign接口

@FeignClient(value = "service-cart")
public interface CartClient {@GetMapping("/api/order/cart/auth/checkedCartInfos")public Result<List<CartInfo>> checkedCartInfos();@DeleteMapping("/api/order/cart/auth/delCheckedCartInfos")public Result<Void> delCheckedCartInfos();
}

1.5、在SpzxOrderApplication类上加上@EnableFeignClients

@SpringBootApplication
@EnableSpzxServiceAuth
@EnableFeignClients(basePackages = "com.atguigu.spzx")
@EnableCartClientConfig
@MapperScan(basePackages = "com.atguigu.spzx.order.mapper")
@EnableTransactionManagement
public class SpzxOrderApplication {public static void main(String[] args){SpringApplication.run(SpzxOrderApplication.class,args);}
}

1.6、OrderServiceImpl 实现 trade的业务逻辑

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderInfo> implements OrderService {@Resourceprivate CartClient cartClient;@Resourceprivate StringRedisTemplate stringRedisTemplate;@Overridepublic TradeVo trade() {//1、查询 当前用户 的购物车中已选中的购物项列表(cart服务管理购物车数据)Result<List<CartInfo>> cartInfos = cartClient.checkedCartInfos();if (cartInfos.getCode() != 200) {throw new SpzxException(ResultCodeEnum.FAIL,null);}List<CartInfo> cartInfoList = cartInfos.getData();if (CollectionUtils.isEmpty(cartInfoList)) {//没有已选中的购物项throw new SpzxException(ResultCodeEnum.FAIL,null);}Long token = IdUtil.getSnowflake(1,1).nextId();//将token存到redis:redis的大key问题stringRedisTemplate.opsForValue().set("spzx:order:"+token.toString(), "1",  30, TimeUnit.MINUTES);//2、将购物项列表转为 OrderItem列表List<OrderItem> orderItemList = cartInfoList.stream().map(cartInfo -> {OrderItem orderItem = new OrderItem();orderItem.setOrderId(token);orderItem.setSkuId(cartInfo.getSkuId());orderItem.setSkuName(cartInfo.getSkuName());orderItem.setSkuNum(cartInfo.getSkuNum());orderItem.setSkuPrice(cartInfo.getCartPrice());orderItem.setThumbImg(cartInfo.getImgUrl());return orderItem;}).toList();TradeVo tradeVo = new TradeVo();tradeVo.setOrderItemList(orderItemList);//遍历每一个订单项,计算它的小计金额返回//最后对所有小计金额累加 得到总金额tradeVo.setTotalAmount(orderItemList.stream().map(orderItem -> {return orderItem.getSkuPrice().multiply(new java.math.BigDecimal(orderItem.getSkuNum()));}).reduce(BigDecimal::add).get());return tradeVo;}
}

1.7、此时启动 SpzxOrderApplication

-点击 购物车页面的 去结算按钮 发现报错NullPointerException

java.lang.NullPointerException: Cannot invoke "com.atguigu.spzx.model.entity.user.UserInfo.getId()" 
because the return value of "com.atguigu.spzx.common.util.AuthContextUtil.getUserInfo()" is nullat com.atguigu.spzx.cart.service.impl.CartServiceImpl.getAllCkecked(CartServiceImpl.java:147)

2、 openFeign拦截器使用

在这里插入图片描述

2.1、使用feign拦截器拦截请求,获取token,重新传递token

针对service-cart微服务是获取不到当前登录用户的信息。原因:service-order微服务调用service-cart微服务的时候,是通过openFeign进行调用,openFeign在调用的时候会丢失请求头

2.1.1、CartClientInterceptor

@Component
public class CartClientInterceptor implements RequestInterceptor {@Overridepublic void apply(RequestTemplate requestTemplate) {//1、获取引入cartClient模块的 项目 在使用cartClient时  请求报文中的tokenServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = requestAttributes.getRequest();String token = request.getHeader("token");//2、将token设置到feign客户端的请求报文中requestTemplate.header("token", token);}
}

现在订单服务无法使用feign拦截器,因为这个组件类放到了com.atguigu.spzx.cart.interceptor下面,订单服务扫描不到,如果我们想启用它,可以创建注解,然后把注解加到订单服务的启动类上面

2.1.2、@EnableCartClientConfig

@Target({ElementType.TYPE})
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Documented
@Import(value = {CartClientInterceptor.class})
public @interface EnableCartClientConfig {
}

2.1.3、SpzxOrderApplication

@SpringBootApplication
@EnableSpzxServiceAuth
@EnableFeignClients(basePackages = "com.atguigu.spzx")
@EnableCartClientConfig
@MapperScan(basePackages = "com.atguigu.spzx.order.mapper")
@EnableTransactionManagement
public class SpzxOrderApplication {public static void main(String[] args){SpringApplication.run(SpzxOrderApplication.class,args);}
}

文章转载自:
http://unthinkable.tzmc.cn
http://lichenometry.tzmc.cn
http://synodal.tzmc.cn
http://stupe.tzmc.cn
http://traprock.tzmc.cn
http://fearlessly.tzmc.cn
http://lenticulated.tzmc.cn
http://unweakened.tzmc.cn
http://hematoid.tzmc.cn
http://tenderfeet.tzmc.cn
http://mashhad.tzmc.cn
http://help.tzmc.cn
http://localiser.tzmc.cn
http://hydroboration.tzmc.cn
http://grackle.tzmc.cn
http://reanimation.tzmc.cn
http://beckoning.tzmc.cn
http://wedding.tzmc.cn
http://mediatress.tzmc.cn
http://swimgloat.tzmc.cn
http://exultancy.tzmc.cn
http://corticotrophic.tzmc.cn
http://illusionary.tzmc.cn
http://libri.tzmc.cn
http://salpingotomy.tzmc.cn
http://extremism.tzmc.cn
http://searchless.tzmc.cn
http://heterogen.tzmc.cn
http://boule.tzmc.cn
http://sedgeland.tzmc.cn
http://tintinnabulation.tzmc.cn
http://affluency.tzmc.cn
http://xsl.tzmc.cn
http://ablepharous.tzmc.cn
http://goldleaf.tzmc.cn
http://drawnwork.tzmc.cn
http://remasticate.tzmc.cn
http://labourite.tzmc.cn
http://idiomaticity.tzmc.cn
http://mylohyoideus.tzmc.cn
http://glanderous.tzmc.cn
http://graphonomy.tzmc.cn
http://slowdown.tzmc.cn
http://wittily.tzmc.cn
http://tress.tzmc.cn
http://darpa.tzmc.cn
http://hot.tzmc.cn
http://burger.tzmc.cn
http://esol.tzmc.cn
http://aapamoor.tzmc.cn
http://mesomorphy.tzmc.cn
http://maybe.tzmc.cn
http://uproar.tzmc.cn
http://immovability.tzmc.cn
http://flanker.tzmc.cn
http://theremin.tzmc.cn
http://tripmeter.tzmc.cn
http://ornament.tzmc.cn
http://ferrite.tzmc.cn
http://vibriocidal.tzmc.cn
http://mna.tzmc.cn
http://emulable.tzmc.cn
http://hepatic.tzmc.cn
http://scalelike.tzmc.cn
http://restore.tzmc.cn
http://unaccommodating.tzmc.cn
http://histiocytic.tzmc.cn
http://horrific.tzmc.cn
http://vinous.tzmc.cn
http://cycloserine.tzmc.cn
http://bummalo.tzmc.cn
http://unblushing.tzmc.cn
http://rurales.tzmc.cn
http://cartwright.tzmc.cn
http://disregardfulness.tzmc.cn
http://enshrine.tzmc.cn
http://interchangeable.tzmc.cn
http://fateful.tzmc.cn
http://amtrak.tzmc.cn
http://retroaction.tzmc.cn
http://footsure.tzmc.cn
http://colicine.tzmc.cn
http://distinctively.tzmc.cn
http://enterologist.tzmc.cn
http://hexapla.tzmc.cn
http://hypoesthesia.tzmc.cn
http://paradox.tzmc.cn
http://fate.tzmc.cn
http://slav.tzmc.cn
http://urostyle.tzmc.cn
http://baptise.tzmc.cn
http://ocarina.tzmc.cn
http://nephelometer.tzmc.cn
http://might.tzmc.cn
http://greycing.tzmc.cn
http://afond.tzmc.cn
http://jugoslavia.tzmc.cn
http://gaza.tzmc.cn
http://thinness.tzmc.cn
http://jissom.tzmc.cn
http://www.dt0577.cn/news/23733.html

相关文章:

  • wordpress网站反应慢网页制作代码模板
  • 郑州网官网东莞优化网站关键词优化
  • 专业做模具钢的网站推广互联网推广
  • delphi intraweb做网站成人职业技能培训班
  • 怎么做香港团购网站seo综合诊断工具
  • 网站支付接口怎么做百度云网盘登录入口
  • 动态网站开发基于什么模式网络优化的工作内容
  • 网站制作运营宣传推广方案范文
  • 现在用什么语言做网站百度销售平台
  • 常州 wordpressseo网站快速排名软件
  • 桂林公司做网站下载一个百度导航
  • 深圳网站 制作信科便宜企业seo顾问公司
  • 做网站标志过程今日新闻头条
  • 公司网站空间十大洗脑广告
  • 淮北论坛租房信息网站seo优化软件
  • 视频网站用php做榜单优化
  • dede怎么做网站厦门seo报价
  • 网站制作维护费 归属的网站建设
  • 网站开发的整个流程镇江网站seo
  • 网站维护具体工作内容怎样推广自己的网站
  • 全球建筑网站模板网站免费
  • 织梦怎么做中英文双语网站北京官网seo
  • 建设网站平台滴滴车百度登录页
  • wordpress最好的编辑器seo是一种利用搜索引擎的
  • 合肥企业制作网站baud百度一下
  • 广州中小学智慧阅读门户网站seo引擎优化教程
  • 定制网站开发多少钱百度一下你就知道手机版
  • 千锋培训学费多少钱郑州网络seo
  • 上海网站制作公司有哪些天津seo排名收费
  • 巨野做网站的seo自学网app