Function函数式路由

        此函数式路由实现方式也是借鉴了小伙伴的实现方式。在开发过程中,总有那么些场景需要我们根据某个字段的不同,需要做不同的逻辑处理,这个时候函数式路由应运而生,项目实操中使用过的方法。

        首先,我们可以使用Function<T,R>函数,他其中的一个方法就是R apply(T t)来实现路由。

        废话不多说,上服务层代码:

package com.zdjx.springbootrabbitmq.roster;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * @Author xiarg
 * @CreateTime 2022/08/27  15:13
 */
@Service
public class QueryGrantTypeService {

    @Autowired
    private GrantTypeService grantTypeService;

    private Map<String, Function<String,String>> grantTypeMap = new HashMap<>();

    @PostConstruct
    public void dispatcherInit(){
        grantTypeMap.put("红包",resourceId -> grantTypeService.redPaper(resourceId));
        grantTypeMap.put("购物券",resourceId -> grantTypeService.shopping(resourceId));
        grantTypeMap.put("qq会员",resourceId -> grantTypeService.qqVip(resourceId));
    }

    public String getResult(String resourceType,String resourceId){
        Function<String,String> result = grantTypeMap.get(resourceType);
        if(result != null){
            return result.apply(resourceId);
        }
        return "查询不到该优惠券的发放方式";
    }
}

这就是路由服务,里面的grantTypeMap 就是存放某个字段,对应后面的方法。其中@PostConstruct是spring容器初始化的时候,会跟随程序一起初始化。下面的getResult方法对应的参数1:resourceType是grantTypeMap的key,即使对应的某个字段,参数2:resourceId即使后面方法的参数,传到下面具体的方法中。 

package com.zdjx.springbootrabbitmq.roster;

import org.springframework.stereotype.Service;

/**
 * @Author xiarg
 * @CreateTime 2022/08/27  15:16
 */
@Service
public class GrantTypeService {

    public String redPaper(String resourceId){
        return "每周末9点发放";
    }
    public String shopping(String resourceId){
        return "每周三11点发放";
    }
    public String qqVip(String resourceId){
        return "每周一13点发放";
    }
}

         这样就实现了,不同字段或对象属性,实现走不同的方法。

        Controller层代码:

package com.zdjx.springbootrabbitmq.controller;

import com.zdjx.springbootrabbitmq.roster.QueryGrantTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author xiarg
 * @CreateTime 2022/08/16  13:50
 */
@RestController
@RequestMapping("/roster")
public class RosterController {


    @Autowired
    private QueryGrantTypeService queryGrantTypeService;

    @GetMapping("/roster/{resourceType}/{resourceId}")
    public String rabbitmq(@PathVariable("resourceType") String resourceType,
                           @PathVariable("resourceId")String resourceId) {
        return queryGrantTypeService.getResult(resourceType,resourceId);
    }

}

到此,函数式路由实现方式已呈现。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值