java整理几种常见的传参方式

本文详细介绍了Java中@QueryParam、@PathParam、@RequestBody、@FormParam和@FormDataParam等在API设计中的应用,包括分页查询、根据ID获取详情、数据增删改查和文件上传等场景,并讨论了各自适用的技术细节和Content-Type设置。
摘要由CSDN通过智能技术生成

1.使用@QueryParam,一般分页查询和根据id获取详情的接口用的较多,直接在请求的url后面拼参数就可以,Content-type使用Application/json的方式传输

 @GET
    @Path("count")
    @RolesAllowed({UserRoleConstants.ADMIN})
    @ApiOperation(value = "查询客户营销统计列表(分页)", tags = {ApiConstants.ADMIN,ApiConstants.CUSTOMER_MARKETING}, response = CustomerUpdateRecord.class, responseContainer = "LIST")
    public Response getCustomerMarketingCountPageList(
            @ApiParam(value = "页码,默认1首页") @QueryParam("pageIndex") Integer pageIndex,
            @ApiParam(value = "每页的数据数量,默认10") @QueryParam("pageSize") Integer pageSize,
            @ApiParam(value = "关键字(姓名/支行名称)") @QueryParam("keywords") String keywords,
            @ApiParam(value = "职位") @QueryParam("position") String position,
            @ApiParam(value = "开始时间") @QueryParam("startTime") String startTime,
            @ApiParam(value = "结束时间") @QueryParam("endTime") String endTime
    ) {
        if (pageIndex == null || pageIndex < 1) pageIndex = 1;
        if (pageSize == null || pageSize < 1) pageSize = 10;
        return ok(customerMarketingService.getCustomerMarketingCountPageList(keywords,position,pageIndex,pageSize,startTime,endTime));
    }

2.使用@PathParam,(此方式的传参方式为必传,如果有些参数不是必传的,这种方式不适用)前端直接将值拼在url上,如: http://localhost:23888/api/customer/111(此处的111即为这个主键的id),Content-type使用Application/json的方式传输

@GET
    @Path("{id}")
    @ApiOperation(value = "根据id查询客户营销", tags = {ApiConstants.ADMIN,ApiConstants.CUSTOMER_MARKETING}, response = CustomerMarketing.class)
    public Response getCustomerMarketingById(@ApiParam(value = "主键id") @PathParam("id") Long id) {
        return ok(customerMarketingService.getById(id));
    }

3.使用@RequestBody,一般像新增保存时,以及牵涉3个以上参数的操作会使用这个接口,Content-type使用Application/json的方式传输

@POST
    @Path("")
    @RolesAllowed({UserRoleConstants.ADMIN})
    @ApiOperation(value = "新增或修改客户营销", tags = {ApiConstants.ADMIN,ApiConstants.CUSTOMER_MARKETING}, response = CustomerMarketing.class)
    @Consumes(MediaType.APPLICATION_JSON)
    public Response addOrUpdateCustomerMarketing(@RequestBody CustomerMarketing customerMarketing, @Context SecurityContext securityContext) {
        UserRolePrincipal userRolePrincipal = (UserRolePrincipal) securityContext.getUserPrincipal();
        customerMarketingService.addOrUpdateCustomerMarketing(customerMarketing,userRolePrincipal);
        return ok();
    }

4.使用@FormParam,一般牵涉到1-3个之内的参数,并且参数的内容可能比较长的时候会使用,Content-type使用application/x-www-form-urlencoded; charset=UTF-8的方式传输,不传charset=UTF-8会中文乱码

@POST
    @Path("opinion")
    @RolesAllowed({UserRoleConstants.ADMIN})
    @ApiOperation(value = "领导点评", tags = {ApiConstants.ADMIN,ApiConstants.CUSTOMER_MARKETING})
    public Response opinionCustomerMarketing(
            @ApiParam(value = "content") @FormParam(value = "content") String content,
            @ApiParam(value = "marketingId") @FormParam(value = "marketingId") Long marketingId,
            @Context SecurityContext securityContext) {
        logger.info("opinionCustomerMarketing.params:content==={};marketingId==={}",content,marketingId);
        if(null == marketingId || 0 == marketingId.intValue()) throw new AppBusinessException(BaseErrorCode.BAD_REQUEST);
        if(StringUtils.isEmpty(content)) throw new AppBusinessException(BaseErrorCode.BAD_REQUEST);
        UserRolePrincipal userRolePrincipal = (UserRolePrincipal) securityContext.getUserPrincipal();
        return ok(customerMarketingService.opinionCustomerMarketing(content,marketingId,userRolePrincipal));
    }

5. 使用@FormDataParam,一般上传文件使用,Content-type使用multipart/form-data

@POST
    @Path("excel/import")
    @RolesAllowed({UserRoleConstants.ADMIN})
    @ApiOperation(value = "excel导入客户数据", tags = {ApiConstants.ADMIN,ApiConstants.CUSTOMER_MARKETING})
    @Consumes({MediaType.MULTIPART_FORM_DATA})
    public Response importProject(@FormDataParam("file") InputStream inputStream,
                                  @FormDataParam("file") FormDataContentDisposition fileDetail,
                                  @Context SecurityContext securityContext) {
        UserRolePrincipal userRolePrincipal = (UserRolePrincipal) securityContext.getUserPrincipal();
        return ok(customerMarketingService.importProject(inputStream,fileDetail,userRolePrincipal));
    }

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值