自定义serviceApi复用于客户端和服务端接口

14 篇文章 0 订阅
14 篇文章 0 订阅

创建工程HelloServiceApi,目录结构如下:

其中entity.User和service.HelloService定义如下:

package com.didispace.helloserviceapi.service;

import com.didispace.helloserviceapi.entity.User;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/refactor")
public interface HelloService {

    // 注意:以下三个方法,必须显示声明参数名称,否则Feign会抛出IIIegalStateException
    @GetMapping("/hello4")
    String hello(@RequestParam("name") String name);

    @GetMapping("/hello5")
    User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);

    @PostMapping("/hello6")
    String hello(@RequestBody User user);

}

 

package com.didispace.helloserviceapi.entity;

public class User {

    private String name;
    private Integer id;

    public User() {
    }

    public User(String name, Integer id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}

打包:mvn install -Dmaven.test.skip=true

然后,引入本地maven仓库:mvn install:install-file -DgroupId=com.didispace -DartifactId=hello-service-api -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -Dfile=/Users/r/IdeaProjects/sc/hello-service-api/target/hello-service-api-0.0.1-SNAPSHOT.jar

然后,在HelloService引入如下:注意groupId, artifactId, version分别对应上面的命令中的值。

		<dependency>
			<groupId>com.didispace</groupId>
			<artifactId>hello-service-api</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>

但是,却报错:cannot resolve symbol "didispace",

错误原因是因为打包时配置错误,必须加入<configuration><skip>true</skip></configuration>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>

重新打包,并且引入本地仓库,就正确如下:

接着,定义RefactorHelloController实现HelloServiceApi.HelloService

package com.sh.sctest.controller;

import com.didispace.helloserviceapi.entity.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RefactorHelloController implements com.didispace.helloserviceapi.service.HelloService {


    @Override
    public String hello(@RequestParam("name") String name) {
        return "hello " + name;
    }

    @Override
    public User hello(@RequestHeader("name") String name,@RequestHeader("age") Integer age) {
        return new User(name, age);
    }

    @Override
    public String hello(@RequestBody User user) {
        return user.toString();
    }
}

然后,改造FeginConsumer,引入新的HelloServiceApi:

        <dependency>
            <groupId>com.didispace</groupId>
            <artifactId>hello-service-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

 

定义新的接口,并指明@FeignClient(value = "Hello-Service")

package com.sc.feignconsumer.service;

import com.didispace.helloserviceapi.service.HelloService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;

@Component
@FeignClient(value = "Hello-Service")
public interface RefactorHelloService extends HelloService {
}

在FeignConsumerController中新增接口:

    @Autowired
    private RefactorHelloService refactorHelloService;

    @GetMapping("/feign-consumer3")
    public String helloConsumer3() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.refactorHelloService.hello("daluo")).append("\n");
        sb.append(this.refactorHelloService.hello("spring", 30)).append("\n");
        sb.append(this.refactorHelloService.hello(new com.didispace.helloserviceapi.entity.User("daluo", 20))).append("\n");
        return sb.toString();
    }

最后效果如下:

 所以,我们并没有在HelloService以及FeginConsumer定义entity.User,也没有在HelloService以及FeginConsumer显示声明RestFul路径/refactor,一切定义都交给HelloServiceApi !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值