自定義serviceApi複用於客戶端和服務端接口

創建工程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 !

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章