easy-http

1.克隆项目

1.1 项目地址

https://gitee.com/xiaoyudeguang/easy-start develop分支

1.2 项目结构

1.3 pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath />
	</parent>


	<groupId>io.github.xiaoyudeguang</groupId>
	<artifactId>easy-start</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>


	<name>easy-start</name>
	<url>https://gitee.com/xiaoyudeguang/easy-start</url>


	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>


	<dependencies>
		<dependency>
			<groupId>io.github.xiaoyudeguang</groupId>
			<artifactId>easy-swagger</artifactId>
			<version>4.0.5</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<mainClass>com.zlyx.demo.App</mainClass>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

2.接口可视化

2.1 浏览器打开地址: http://localhost:8080/easy-swagger

2.2 调试接口

2.2.1 调用方接口

 

2.2.2 被调用接口

2.3 接口代码

package com.zlyx.easy.start.controller;


import java.util.Map;


import com.zlyx.easy.core.loggers.Logger;
import com.zlyx.easy.core.model.ResultModel;
import com.zlyx.easy.core.utils.HttpUtils.HttpClient;
import com.zlyx.easy.core.utils.HttpUtils.HttpResponse;
import com.zlyx.easy.core.utils.RequestUtils;
import com.zlyx.easy.swagger.annotations.SpringController;
import com.zlyx.easy.swagger.annotations.SpringMapping;


/**
 * Http测试接口
 * 
 * @Auth 赵光
 * @Describle
 * @2019年1月3日 下午2:54:41
 */
@SpringController(value = "/test", todo = { "easy-http接口使用示例" })
public class HttpTestController {


	/**
	 * 被调用的http接口
	 * 
	 * @param params
	 * @return
	 */
	@SpringMapping(value = "service", todo = "被调用的HTTP接口")
	public ResultModel<Map<String, String>> service(String id, String name, int age) {
		return ResultModel.success(RequestUtils.getParamsMap());
	}


	/**
	 * 调用方http接口
	 * 
	 * @param params
	 * @return
	 */
	@SpringMapping(value = "callService", todo = "调用方HTTP接口")
	public HttpResponse callService(String id, String name, int age) {
		String url = "http://localhost:8080/test/service";
		try {
			return HttpClient.url(url).param("id", id).param("name", name).param("age", age).post();
		} catch (Exception e) {
			Logger.err(e.getMessage(), e);
			return HttpResponse.failure(e);
		}
	}
}
  1.  

3.玩转easy-http

3.1 客户端编程示例(POST)

3.1.1 客户端代码

package com.zlyx.easy.start.test;


import com.zlyx.easy.core.utils.HttpUtils.HttpClient;
import com.zlyx.easy.core.utils.OptUtils;


public class HttpTest {


	public static void main(String[] args) throws Exception {
		String id = OptUtils.randomUUID();
		String name = id + "_" + OptUtils.getMillis();
		int age = OptUtils.randomNum();
		String url = "http://localhost:8080/test/service";
		HttpClient.url(url).param("id", id).param("name", name).param("age", age).put();
	}
}

PS:需要修改SpringMapping注解的method值,默认为post。

3.1.2 运行截图

3.2 客户端编程示例(GET)

3.2.1 客户端代码

package com.zlyx.easy.start.test;


import com.zlyx.easy.core.utils.HttpUtils.HttpClient;
import com.zlyx.easy.core.utils.OptUtils;


public class HttpTest {


	public static void main(String[] args) throws Exception {
		String id = OptUtils.randomUUID();
		String name = id + "_" + OptUtils.getMillis();
		int age = OptUtils.randomNum();
		String url = "http://localhost:8080/test/service";
		HttpClient.url(url).param("id", id).param("name", name).param("age", age).get();
    }

}

PS:需要修改SpringMapping注解的method值,默认为post。

3.2.2 运行截图

 

  1. 4.接口式编程

4.1 HttpTestService

package com.zlyx.easy.start.http;


import org.springframework.web.bind.annotation.PostMapping;


import com.zlyx.easy.core.utils.HttpUtils.HttpResponse;
import com.zlyx.easy.http.annotations.HttpService;


@HttpService(value = "http://localhost:8080/")
public interface HttpTestService {


	@PostMapping("/test/service")
	public HttpResponse callService(String name, String id, int age);
}

4.2 HttpServiceController

package com.zlyx.easy.start.controller;


import org.springframework.beans.factory.annotation.Autowired;


import com.zlyx.easy.core.utils.HttpUtils.HttpResponse;
import com.zlyx.easy.start.http.HttpTestService;
import com.zlyx.easy.swagger.annotations.SpringController;
import com.zlyx.easy.swagger.annotations.SpringMapping;


/**
 * Http测试接口
 * 
 * @Auth 赵光
 * @Describle
 * @2019年1月3日 下午2:54:41
 */
@SpringController(value = "/http", todo = { "easy-http使用示例" })
public class HttpServiceController {


	@Autowired
	private HttpTestService httpTestService;


	/**
	 * 调用方http接口
	 * 
	 * @param params
	 * @return
	 */
	@SpringMapping(value = "service", todo = "调用方HTTP接口")
	public HttpResponse callService(String id, String name, int age) {
		return httpTestService.callService(name, id, age);
    }
}

4.3 运行截图

5.easy-http接口式编程原理

5.1 声明一个注解

package com.zlyx.easy.http.annotations;


import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


import org.springframework.stereotype.Component;


/**
 * Http服务接口注解
 * 
 * @Auth 赵光
 * @Describle
 * @2019年12月25日
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Component
public @interface HttpService {


	/**
	 * 服务端域名或IP+端口
	 * 
	 * @return
	 */
	String value();
}

5.2 接口上使用

package com.zlyx.easy.start.http;


import org.springframework.web.bind.annotation.PostMapping;


import com.zlyx.easy.core.utils.HttpUtils.HttpResponse;
import com.zlyx.easy.http.annotations.HttpService;


@HttpService(value = "http://localhost:8080/")
public interface HttpTestService {


	@PostMapping("/test/service")
	public HttpResponse callService(String name, String id, int age);
}

5.3 实现easy-core封装后的自动配置接口,在execute()方法中完成后续调用链

package com.zlyx.easy.http.dispatch;


import java.lang.reflect.Method;


import org.springframework.beans.factory.annotation.Autowired;


import com.zlyx.easy.core.factory.FactoryBeanConfigurer;
import com.zlyx.easy.core.factory.defaults.annotations.FactoryBean;
import com.zlyx.easy.core.factory.interfaces.FactoryBeanDefiner;
import com.zlyx.easy.core.spring.SpringUtils;
import com.zlyx.easy.http.annotations.HttpService;
import com.zlyx.easy.http.exceptions.MappingHandlerException;
import com.zlyx.easy.http.handlers.AbstractMappingHandler;
import com.zlyx.easy.http.models.RequestModel;
import com.zlyx.easy.http.parser.HttpMethodParser;
import com.zlyx.easy.http.parser.defaults.DefaultHttpMethodParser;


/**
 * 请求路由
 * 
 * @Auth 赵光
 * @Describle
 * @2019年12月25日
 */
@FactoryBean(annotationClass = HttpService.class, todo = "请求转发")
public class HttpMappingDispatcher implements FactoryBeanDefiner {


	@Autowired(required = false)
	private HttpMethodParser httpMethodParser;


	@Override
	public Object excute(Class<?> proxyClass, Method method, Object[] args) throws Exception {
		RequestModel rm = httpMethodParser.parse(proxyClass, method, args);
		rm.setBaseUrl(SpringUtils.getProperty(rm.getBaseUrl()));
		if (rm.getUrl() == null) {
			throw new MappingHandlerException("请求地址不能为空!");
		}
		if (rm.getMethod() == null) {
			throw new MappingHandlerException("请求方式不能为空!");
		}
		return AbstractMappingHandler.call(rm);
	}


	@Override
	public void afterPropertiesSet() throws Exception {
		FactoryBeanConfigurer.addFactoryBeanHandler(this);
		if (httpMethodParser == null) {
			this.httpMethodParser = new DefaultHttpMethodParser();
		}
      }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值