32 Star 134 Fork 45

leecho / spring-cloud-feign-proxy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Introduction

This project can automatically expose FeignClients to Rest without MVC controller. Use CGLIB dynamic proxy service interface to RestController.

Feature

  • Auto export feign without MVC controller
  • Can specify the package path for scanning.
  • Support Swagger

Get Started

Create Interface as Feign Client

/**
 * @author liqiu
 * @create 2018/9/21 13:52
 **/
@Api(tags = "DemoService", description = "Demo Feign Client")
@FeignClient("demo-service")
public interface DemoService {

	/**
	 * create demo
	 *
	 * @param demo
	 * @return
	 */
	@ApiOperation(value = "Create demo")
	@PostMapping(value = "/demo")
	Demo create(@RequestBody @Valid @ApiParam Demo demo);

	/**
	 * update demo
	 *
	 * @param demo
	 * @return
	 */
	@PutMapping(value = "/demo")
	Demo update(@RequestBody @Valid Demo demo);

	/**
	 * delete demo
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/demo/{id}")
	Demo delete(@PathVariable(name = "id") String id);

	/**
	 * list demo
	 *
	 * @param id
	 * @param headerValue test header value
	 * @return
	 */
	@GetMapping(value = "/demo/{id}")
	Demo get(@PathVariable(name = "id") String id, @RequestHeader(name = "header") String headerValue);

	/**
	 * list demos
	 *
	 * @return
	 */
	@GetMapping(value = "/demos")
	List<Demo> list();

	/**
	 * upload file
	 *
	 * @param file
	 * @return
	 */
	@PostMapping(value = "/demo/upload")
	String upload(@RequestPart(name = "file") MultipartFile file);
}

Create Implementor

@Slf4j
@Primary
@Service
public class DemoServiceImpl implements DemoService {

	@Override
	public Demo create(Demo demo) {
		log.info("Create executed : " + demo);
		return demo;
	}

	@Override
	public Demo update(Demo demo) {
		log.info("Update execute :" + demo);
		return demo;
	}

	@Override
	public Demo delete(String id) {
		log.info("Delete execute : " + id);
		return Demo.builder().name("demo-" + id).data("data-" + id).build();
	}

	@Override
	public Demo get(String id, String header) {
		Demo demo = Demo.builder()
				.name(header)
				.data(header).build();
		System.out.println("Get execute : " + id + "," + header);
		return demo;
	}

	@Override
	public List<Demo> list() {
		System.out.println("List execute");
		List<Demo> demos = new ArrayList<>();
		for (int i = 0; i < 5; i++) {
			Demo demo = Demo.builder()
					.name("demo-" + i)
					.data("data" + i).build();
			demos.add(demo);
		}
		return demos;
	}

	@Override
	public String upload(MultipartFile file) {
		return file.getOriginalFilename();
	}
}

Enable Feign Proxy for Application

@EnableFeignProxies(basePackages = "com.github.leecho")
@ComponentScan("com.github.leecho")
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

Run Test Case

mvn test

Console Output

2018-11-13 18:42:12.027  INFO 2124 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo/{id}],methods=[GET]}" onto public final com.github.leecho.spring.feign.sample.model.Demo com.github.leecho.spring.feign.sample.service.DemoService$$EnhancerByCGLIB$$bf7785b6.get(java.lang.String,java.lang.String)
2018-11-13 18:42:12.027  INFO 2124 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo],methods=[PUT]}" onto public final com.github.leecho.spring.feign.sample.model.Demo com.github.leecho.spring.feign.sample.service.DemoService$$EnhancerByCGLIB$$bf7785b6.update(com.github.leecho.spring.feign.sample.model.Demo)
2018-11-13 18:42:12.028  INFO 2124 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo/{id}],methods=[DELETE]}" onto public final com.github.leecho.spring.feign.sample.model.Demo com.github.leecho.spring.feign.sample.service.DemoService$$EnhancerByCGLIB$$bf7785b6.delete(java.lang.String)
2018-11-13 18:42:12.028  INFO 2124 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo],methods=[POST]}" onto public final com.github.leecho.spring.feign.sample.model.Demo com.github.leecho.spring.feign.sample.service.DemoService$$EnhancerByCGLIB$$bf7785b6.create(com.github.leecho.spring.feign.sample.model.Demo)
2018-11-13 18:42:12.028  INFO 2124 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demos],methods=[GET]}" onto public final java.util.List com.github.leecho.spring.feign.sample.service.DemoService$$EnhancerByCGLIB$$bf7785b6.list()
2018-11-13 18:42:12.029  INFO 2124 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo/upload],methods=[POST]}" onto public final java.lang.String com.github.leecho.spring.feign.sample.service.DemoService$$EnhancerByCGLIB$$bf7785b6.upload(org.springframework.web.multipart.MultipartFile)

Issues

  • @PathVariable @HeaderValue @CookieValue must use attr "name" for special
  • The real implementor of service interface must use @Primary to avoid bean conflicts
MIT License Copyright (c) 2020 leecho Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

自动生成FeignClients的HTTP接口,无需单独编写Controller类暴露HTTP接口。 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/leecho/spring-cloud-feign-proxy.git
git@gitee.com:leecho/spring-cloud-feign-proxy.git
leecho
spring-cloud-feign-proxy
spring-cloud-feign-proxy
master

搜索帮助