关于req.params、req.param(name)、req.query、req.body等请求对象

本文详细介绍了HTTP请求对象中常用的属性和方法,包括req.params、req.query、req.body等,并解释了它们在处理GET和POST请求中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请求对象,通常传递到回调方法,这意味着你可以随意命名,通常命名为 req 或 request 。

请求对象中最常用的属性和方法有:

req.params
一个数组,包含命名过的路由参数。

req.param(name)
返回命名的路由参数,或者 GET 请求或 POST 请求参数。建议你忽略此方法。

req.query
一个对象,包含以键值对存放的查询字符串参数(通常称为 GET 请求参数) 。

req.body
一个对象,包含 POST 请求参数。这样命名是因为 POST 请求参数在 REQUEST 正文中传递,而不像查询字符串在 URL 中传递。要使 req.body 可用,需要中间件能够解析请求正文内容类型。

req.route
关于当前匹配路由的信息。主要用于路由调试。

req.cookies/req.singnedCookies
一个对象,包含从客户端传递过来的 cookies 值。

req.headers
从客户端接收到的请求报头。

req.accepts([types])
一个简便的方法,用来确定客户端是否接受一个或一组指定的类型(可选类型可以是单个的 MIME 类型,如 application/json 、一个逗号分隔集合或是一个数组) 。写公共API 的人对该方法很感兴趣。假定浏览器默认始终接受 HTML。

req.ip
客户端的 IP 地址。

req.path
请求路径(不包含协议、主机、端口或查询字符串) 。

req.host
一个简便的方法,用来返回客户端所报告的主机名。这些信息可以伪造,所以不应该用于安全目的。

req.xhr
一个简便属性,如果请求由 Ajax 发起将会返回 true 。

req.protocol
用于标识请求的协议( http 或 https ) 。

req.secure
一个简便属性,如果连接是安全的,将返回 true 。等同于req.protocol===’https’ 。

req.url/req.originalUrl
有点用词不当,这些属性返回了路径和查询字符串(它们不包含协议、主机或端口) 。req.url 若是出于内部路由目的,则可以重写,但是req.orginalUrl 旨在保留原始请求和查询字符串。

req.acceptedLanguages
一个简便方法,用来返回客户端首选的一组(人类的)语言。这些信息是从请求报头中解析而来的。

package com.foonsu.efenxiao.platform.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.foonsu.efenxiao.biz.vo.FailOrder; import com.foonsu.efenxiao.common.enums.ShopPlatformClassType; import com.foonsu.efenxiao.common.enums.ShopPlatformType; import com.foonsu.efenxiao.common.exception.CommonErrorCode; import com.foonsu.efenxiao.common.exception.ServiceException; import com.foonsu.efenxiao.common.oss.OSSApiService; import com.foonsu.efenxiao.common.utils.CollectionsUtil; import com.foonsu.efenxiao.common.utils.ExceptionUtil; import com.foonsu.efenxiao.platform.dto.CidGoodsBatchUpdateDTO; import com.foonsu.efenxiao.platform.dto.SyncGoodsSearchDTO; import com.foonsu.efenxiao.platform.entity.PlatformGoods; import com.foonsu.efenxiao.platform.entity.PlatformGoodsSku; import com.foonsu.efenxiao.platform.entity.Shop; import com.foonsu.efenxiao.platform.handler.A1688GoodsLinkResolver; import com.foonsu.efenxiao.platform.handler.CidGoodsHandler; import com.foonsu.efenxiao.platform.platform.PlatformStarter; import com.foonsu.efenxiao.platform.service.IPlatformGoodsDistributeService; import com.foonsu.efenxiao.platform.service.IPlatformGoodsService; import com.foonsu.efenxiao.platform.service.IPlatformGoodsSkuService; import com.foonsu.efenxiao.platform.service.IShopService; import com.foonsu.efenxiao.platform.vo.*; import com.foonsu.efenxiao.system.dto.TenantDTO; import com.foonsu.efenxiao.user.feign.IFactorySellerRelClient; import com.foonsu.efenxiao.user.feign.IUserClient; import com.foonsu.efenxiao.user.feign.pdd.IPddFactorySellerRelClient; import com.foonsu.efenxiao.user.feign.pdd.IPddUserClient; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springblade.core.boot.ctrl.BladeController; import org.springblade.core.mp.support.Condition; import org.springblade.core.mp.support.Query; import org.springblade.core.secure.BladeUser; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.api.R; import org.springblade.core.tool.utils.CollectionUtil; import org.springblade.core.tool.utils.DateUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.validation.Valid; import java.io.Serializable; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; /** * 控制器 * * @author BladeX * @since 2022-03-17 */ @Slf4j @RestController @RequestMapping("/platformgoods") @Api(value = "商品", tags = "商品") public class PlatformGoodsController extends BladeController { @Autowired private IPlatformGoodsService platformGoodsService; @Autowired private A1688GoodsLinkResolver a1688GoodsLinkResolver; @Autowired private IPlatformGoodsSkuService platformGoodsSkuService; @Autowired private IPlatformGoodsDistributeService platformGoodsDistributeService; @Autowired private PlatformStarter platformStarter; @Autowired private IShopService shopService; @Autowired private IFactorySellerRelClient factorySellerRelClient; @Autowired private ExceptionUtil exceptionUtil; @Autowired private IPddFactorySellerRelClient pddFactorySellerRelClient; @Value("${efenxiao.platformClass}") private ShopPlatformClassType platformClassType; @Autowired @Qualifier("goodsSyncThreadPoolTaskExecutor") private ThreadPoolTaskExecutor goodsSyncThreadPoolTaskExecutor; @Autowired private IUserClient userClient; @Resource private IPddUserClient pddUserClient; @Resource private CidGoodsHandler cidGoodsHandler; /** * 自定义分页 */ @GetMapping("/page") @ApiOperationSupport(order = 3) @ApiOperation(value = "分页(旧)", notes = "此接口需要删除,已经切换到pageNew") public R<IPage<PlatformGoodsVO>> page(PlatformGoodsReq req, Query query) { if(null == req || null == query){ throw new ServiceException(CommonErrorCode.PARAM_INVALID); } BladeUser user = AuthUtil.getUser(); req.setTenantId(user.getTenantId()); decorateReq(req); IPage<PlatformGoodsVO> pages = platformGoodsService.platformGoodsPage(Condition.getPage(query), req); return R.data(pages); } private void decorateReq(PlatformGoodsReq req){ if(StringUtils.isNotBlank(req.getProductOutIds())) { String productOutIdsStr = req.getProductOutIds().replaceAll(",", ","); String[] productOutIds = productOutIdsStr.split(","); req.setProductOutIdList(Arrays.asList(productOutIds)); } if(StringUtils.isNotBlank(req.getFactoryTenantId())) { String[] factoryTenantIds = req.getFactoryTenantId().split(","); req.setFactoryTenantIds(Arrays.asList(factoryTenantIds)); req.setFactoryTenantId(req.getFactoryTenantIds().get(0)); } if(StringUtils.isNotBlank(req.getSellerTenantId())) { String[] sellerTenantIds = req.getSellerTenantId().split(","); req.setSellerTenantIds(Arrays.asList(sellerTenantIds)); req.setSellerTenantId(req.getSellerTenantIds().get(0)); } if(StringUtils.isNotBlank(req.getShopBizId())) { String[] shopBizIds = req.getShopBizId().split(","); req.setShopBizIds(Arrays.asList(shopBizIds)); req.setShopBizId(null); } if (StringUtils.isNotBlank(req.getSkuOutIds())) { String[] skuOutIds = req.getSkuOutIds().split(","); req.setSkuOutIdList(Arrays.asList(skuOutIds)); req.setSkuOutIds(null); } } @GetMapping("/pageNew") @ApiOperationSupport(order = 3) @ApiOperation(value = "分页", notes = "传入platformGoodsReq") public R<IPage<PlatformGoodsVO>> pageNew(PlatformGoodsReq req, Query query) { if(null == req || null == query){ throw new ServiceException(CommonErrorCode.PARAM_INVALID); } BladeUser user = AuthUtil.getUser(); req.setTenantId(user.getTenantId()); decorateReq(req); IPage<PlatformGoodsVO> pages = platformGoodsService.platformGoodsPageNew(Condition.getPage(query), req, user); return R.data(pages); } /** * 同步商品 */ @PostMapping("/syncGoods") @ApiOperationSupport(order = 8) @ApiOperation(value = "同步商品", notes = "传入bizId") public R<List<GoodsSyncResp>> syncGoods(){ BladeUser user = AuthUtil.getUser(); List<String> tenantIds = ShopPlatformClassType.PDD == platformClassType? pddFactorySellerRelClient.queryAllParentTenantId(user.getTenantId()).getData(): factorySellerRelClient.queryAllParentTenantId(user.getTenantId()).getData(); List<Shop> shopListResult = shopService.getByTenantIds(tenantIds); R<List<TenantDTO>> tenantListR = platformClassType == ShopPlatformClassType.PDD ? pddUserClient.queryUserTags("cid") : userClient.queryUserTags("cid"); List<String> tenantIdList = tenantListR.getData().stream().map(TenantDTO::getTenantId).collect(Collectors.toList()); List<GoodsSyncResp> result = new ArrayList<>(); for (Shop shop : shopListResult) { GoodsSyncResp goodsSyncResp = new GoodsSyncResp(); if (ShopPlatformType.ZJ.getCode().equals(shop.getShopPlatform())) { continue; } if (shop.getAccessTokenTime().isBefore(LocalDateTime.now())) { goodsSyncResp.setSuccess(Boolean.FALSE); goodsSyncResp.setShopName(shop.getShopName()); goodsSyncResp.setErrorMsg("accessToken过期,请重新授权该店铺"); result.add(goodsSyncResp); shopListResult = shopListResult.stream().filter(shopInfo -> !shop.getBizId().equals(shopInfo.getBizId())).collect(Collectors.toList()); } } SyncGoodsParam syncGoodsParam = new SyncGoodsParam(); if (CollectionsUtil.isNotEmpty(tenantIdList)) { syncGoodsParam.setCidFlag(tenantIdList.contains(tenantIds.get(0))); } // 异步同步商品 platformStarter.asynGoods(shopListResult, syncGoodsParam); // List<Future<GoodsSyncResp>> futures = new ArrayList<>(); // for(Shop shop: shopListResult){ // Future<GoodsSyncResp> future = goodsSyncThreadPoolTaskExecutor.submit(()->{ // GoodsSyncResp res = platformStarter.syncGoods(shop, new SyncGoodsParam()); // return res; // }); // futures.add(future); // } // // for(Future<GoodsSyncResp> future: futures){ // try { // GoodsSyncResp res = future.get(); // log.info("GoodsSyncResp,{}", JSON.toJSONString(res)); // result.add(res); // } catch (Exception e) { // log.error("" ,e); // return R.fail(exceptionUtil.translateException(e)); // } // } return R.data(result); } /** * 修改商品简称或重量 */ @PostMapping("/updateGoodsAndSkuShotName") @ApiOperationSupport(order = 9) @ApiOperation(value = "修改商品简称或重量", notes = "传入platformGoodsVo") public R updateGoodsAndSkuShotName(@Valid @RequestBody PlatformGoodsUpdateReq req) { if (req == null || (req.getId() == null && CollectionsUtil.isEmpty(req.getSkuList()))) { throw new ServiceException(CommonErrorCode.PARAM_INVALID); } BladeUser user = AuthUtil.getUser(); List<PlatformGoodsUpdateReq> reqs = new ArrayList<>(); reqs.add(req); platformGoodsService.batchUpdateGoodsAndSkuShotName(user.getUserId(), reqs); return R.status(true); } /** * 批量修改商品简称或重量 * @param req 请求 * @return 返回 */ @PostMapping("/batchUpdateGoodsAndSkuShotName") @ApiOperationSupport(order = 9) @ApiOperation(value = "批量修改商品简称或重量", notes = "传入platformGoodsVo") public R<Boolean> batchUpdateGoodsAndSkuShotName(@Valid @RequestBody List<PlatformGoodsUpdateReq> req) { if (CollectionUtil.isEmpty(req)) { throw new ServiceException(CommonErrorCode.PARAM_INVALID); } BladeUser user = AuthUtil.getUser(); return R.status(platformGoodsService.batchUpdateGoodsAndSkuShotName(user.getUserId(), req)); } /** * 商品或规格批量绑定厂家 */ @PostMapping("/batchBindFactoryGoods") @ApiOperationSupport(order = 10) @ApiOperation(value = "商品或规格批量绑定厂家", notes = "传入goodsDistributeVOList") public R<Boolean> batchBindFactoryGoods(@Valid @RequestBody GoodsDistributeReq req) { if(null == req || CollectionsUtil.isEmpty(req.getDistributeList())){ throw new ServiceException(CommonErrorCode.PARAM_INVALID); } BladeUser user = AuthUtil.getUser(); //商品或规格批量绑定厂家 platformGoodsDistributeService.batchBindFactoryGoods(req, user); return R.status(true); } /** * 解绑对应厂家的商品关系 * @param req * @return */ @PostMapping("/unboundPlatformGoodsFactory") @ApiOperationSupport(order=11) @ApiOperation(value="解绑对应厂家的商品关系", notes = "distributeReq") public R<Boolean> unboundPlatformGoodsFactory(@RequestBody GoodsDistributeReq req) { if(null == req || CollectionsUtil.isEmpty(req.getDistributeList())){ throw new ServiceException(CommonErrorCode.PARAM_INVALID); } BladeUser user = AuthUtil.getUser(); platformGoodsDistributeService.unboundPlatformGoodsFactory(req.getDistributeList(), user); return R.status(true); } @GetMapping("/lastSyncTime") @ApiOperationSupport(order = 12) @ApiOperation(value = "获取同步时间", notes = "") public R<Long> lastSyncTime() { BladeUser user = AuthUtil.getUser(); List<String> tenantIds = ShopPlatformClassType.PDD == platformClassType? pddFactorySellerRelClient.queryAllParentTenantId(user.getTenantId()).getData(): factorySellerRelClient.queryAllParentTenantId(user.getTenantId()).getData(); List<Shop> shopListResult = shopService.getByTenantIds(tenantIds); long time = shopListResult.stream().mapToLong(t->t.getGoodsSyncTime()==null?0L: DateUtil.toMilliseconds(t.getGoodsSyncTime())).max().orElseGet(()->0L); return R.data(time); } @PostMapping(value = "setDistributePrice") @ApiOperationSupport(order = 13) @ApiOperation(value = "设置分销价格", notes = "") public R<Boolean> setDistributePrice(@RequestBody GoodsDistributePriceReq req){ BladeUser user = AuthUtil.getUser(); return R.status(platformGoodsDistributeService.setDistributePrice(req,user)); } @PostMapping(value = "saveLocalGoods") @ApiOperationSupport(order = 14) @ApiOperation(value = "保存手工商品", notes = "") public R<Boolean> saveLocalGoods(@RequestBody LocalGoodsSaveReq req){ BladeUser user = AuthUtil.getUser(); checkParam(req); Set<String> usedImgs = new HashSet<>(); platformGoodsService.saveLocalGoods(user, req, usedImgs); userClient.uploadImgUsed(usedImgs, OSSApiService.goodsFolderName); return R.status(true); } @PostMapping(value = "batchSaveLocalGoods") @ApiOperationSupport(order = 18) @ApiOperation(value = "批量保存手工商品", notes = "") public R<List<FailOrder>> batchSaveLocalGoods(@RequestBody List<LocalGoodsSaveReq> reqs){ List<FailOrder> failOrders = new ArrayList<>(); for (LocalGoodsSaveReq req : reqs) { try{ this.saveLocalGoods(req); }catch (Exception e){ if (!(e instanceof Serializable)){ log.error("批量保存手工商品报错,{}" ,req.getProductName(), e); } FailOrder failOrder = new FailOrder(req.getProductName(), exceptionUtil.translateException(e)); failOrders.add(failOrder); } } return R.data(failOrders); } @PostMapping("/queryGoodsForSelect") @ApiOperationSupport(order = 15) @ApiOperation(value = "选择商品") public R<IPage<PlatformGoodsJoinSkuResp>> queryGoodsForSelect(@RequestBody LocalGoodsQueryReq req) { BladeUser user = AuthUtil.getUser(); IPage<PlatformGoodsJoinSkuResp> pages = platformGoodsService.queryGoodsForSelect(user, req); return R.data(pages); } @PostMapping("/deleteGoods") @ApiOperationSupport(order = 16) @ApiOperation(value = "删除手工商品") public R<Boolean> deleteGoods(@RequestBody LocalGoodsDeleteReq req) { BladeUser user = AuthUtil.getUser(); if (req.getGoodsBizIds() == null || req.getGoodsBizIds().size() == 0 ){ throw new ServiceException(CommonErrorCode.PARAM_INVALID); } if (req.getSkuBizIds() == null){ req.setSkuBizIds(new ArrayList<>()); } platformGoodsService.deleteGoods(req, user); return R.data(true); } private void checkParam(LocalGoodsSaveReq req) { if (StringUtils.isEmpty(req.getProductName()) || req.getShopBizId()==null){ throw new ServiceException(CommonErrorCode.PARAM_INVALID); } if (req.getSkus() != null && req.getSkus().size() > 0){ for (LocalGoodsSaveSkuReq sku : req.getSkus()) { if (StringUtils.isEmpty(sku.getSkuName())){ throw new ServiceException(CommonErrorCode.PARAM_INVALID); } } } } @PostMapping("/parseAlibabaGoodsLink") @ApiOperationSupport(order = 17) @ApiOperation(value = "解析阿里巴巴商品链接") public R<AlibabaResolveGoodsLinkResp> parseAlibabaGoodsLink(@RequestBody AlibabaResolveGoodsLinkReq req) { BladeUser user = AuthUtil.getUser(); if (StringUtils.isBlank(req.getLinkUrl()) ){ throw new ServiceException(CommonErrorCode.PARAM_INVALID); } AlibabaResolveGoodsLinkResp resp = a1688GoodsLinkResolver.parseAlibabaGoodsLink(user, req.getLinkUrl()); return R.data(resp); } @PostMapping("/bindAlibabaGoods") @ApiOperationSupport(order = 18) @ApiOperation(value = "绑定阿里巴巴商品") public R<Boolean> bindAlibabaGoods(@RequestBody AlibabaBindGoodsReq req) { BladeUser user = AuthUtil.getUser(); if (StringUtils.isBlank(req.getAlibabaProductId()) || StringUtils.isBlank(req.getFactoryTenantId()) || StringUtils.isBlank(req.getShopOauthId()) || req.getBindItems() == null || req.getBindItems().size() == 0 ){ throw new ServiceException(CommonErrorCode.PARAM_INVALID); } platformGoodsDistributeService.bindAlibabaGoods(user, req); return R.data(true); } @GetMapping("/getPlatformGoodsList") public R<List<LogisticsMatchPlatformGoodsVO>> getPlatformGoodsList(PlatformGoodsReq req) { BladeUser user = AuthUtil.getUser(); req.setTenantId(user.getTenantId()); req.setPageStart(1L); req.setPageSize(10000L); List<LogisticsMatchPlatformGoodsVO> platformGoodsVOS = platformGoodsService.queryPlatformGoodsByTenantId(req); return R.data(platformGoodsVOS); } @PostMapping("/getPlatformGoodsListByPage") @ResponseBody public R<Page<PlatformGoods>> getPlatformGoodsListByPage(@RequestBody SyncGoodsSearchDTO syncOrderGoodsDTO) { Page<PlatformGoods> platformGoodsPage = platformGoodsService.queryPlatformGoodsBySync(syncOrderGoodsDTO); return R.data(platformGoodsPage); } @PostMapping("/getPlatformGoodsSkuListByPage") @ResponseBody public R<Page<PlatformGoodsSku>> getPlatformGoodsSkuListByPage(@RequestBody SyncGoodsSearchDTO syncOrderGoodsDTO) { Page<PlatformGoodsSku> platformGoodsSkuPage = platformGoodsSkuService.queryPlatformGoodsSkuBySync(syncOrderGoodsDTO); return R.data(platformGoodsSkuPage); } @PostMapping("/batchUpdateCidGoodsInfo") @ResponseBody public R<Boolean> batchUpdateCidGoodsInfo(@RequestBody List<CidGoodsBatchUpdateDTO> cidGoodsBatchUpdateDTOS) { BladeUser user = AuthUtil.getUser(); Boolean aBoolean = platformGoodsService.batchUpdateCidGoodsInfo(user.getTenantId(), cidGoodsBatchUpdateDTOS); return R.data(aBoolean); } @GetMapping("/cidPage") @ApiOperationSupport(order = 3) @ApiOperation(value = "分页", notes = "传入platformGoodsReq") public R<IPage<PlatformGoodsVO>> cidPage(PlatformGoodsReq req, Query query) { if (null == req || null == query) { throw new ServiceException(CommonErrorCode.PARAM_INVALID); } BladeUser user = AuthUtil.getUser(); req.setTenantId(user.getTenantId()); decorateReq(req); IPage<PlatformGoodsVO> pages = platformGoodsService.platformGoodsPageByCid(Condition.getPage(query), req, user); return R.data(pages); } @PostMapping("/importCidGoodsFile") public R<Boolean> importCidGoodsFile(MultipartFile file) { BladeUser user = AuthUtil.getUser(); cidGoodsHandler.importCidGoods(file, user.getTenantId()); return R.success("导入成功"); } @PostMapping("/importCidOrderGoodsFile") public R<Boolean> importCidOrderGoodsFile(MultipartFile file) { BladeUser user = AuthUtil.getUser(); cidGoodsHandler.importCidOrderGoodsFile(file, user.getTenantId()); return R.success("导入成功"); } } 解释一下 我代码能力有点差
最新发布
07-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI小美好

感恩打赏让我坚定努力的方向

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值