改微服务步骤及遇到的问题处理RequestParam.value() was empty on parameter 0

背景:将原来的服务改成微服务后,暴露接口给其他服务调用,启动时报错RequestParam.value() was empty on parameter 0

顺便提下改为服务步骤:

1.新建一个model,只提供接口对外暴露(同原来controller类似),这里用XxxApi表示

2.在新建的model中导入以下包(可根据需要导)

 <!-- cloud | eureka -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

3.然后将原来service实现类改为implements “对外暴露接口XxxApi”,其他不用改动,原来多余的service接口可以删除

4.调用方操作步骤

a.微服务对应的包要有,和上面2一样(有人非要说没有明确server、client,要导不同的包也可以),我这里导xxx-starter是因为各微服务之间需要相互调用

b.上面1新建的model对应的jar包不用说也要导入

c.在Application启动类加上@EnableFeignClients扫包配置(如下)

@EnableFeignClients(basePackages = {
    "cn.xxx.xxx.api",
    "com.xxx.xxx.api"
})

d.接下来在业务代码实现类中 @Autowired XxxApi xxxApi;再调用相对应的功能即可

遇到的坑:
XxxApi接口使用(@RequestParam Integer flg),调用方启动时报错:RequestParam.value() was empty on parameter 0

修改:XxxApi接口,(@RequestParam("flg") Integer flg)即可

原因:见这篇博客https://blog.csdn.net/justry_deng/article/details/80785973

如有不当之处,请指出!!

发布了43 篇原创文章 · 获赞 13 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_41070393/article/details/88124773