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();
		}
      }
}

 

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