Function函数式路由

导读:本篇文章讲解 Function函数式路由,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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

        首先,我们可以使用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);
    }

}

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/101752.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!