Merge branch 'master' into registry-nacos

pull/1/head
tanghc 5 years ago
commit 734265601c
  1. 8
      changelog.md
  2. 2
      sop-admin/sop-admin-server/pom.xml
  3. 2
      sop-common/pom.xml
  4. 4
      sop-common/sop-gateway-common/pom.xml
  5. 5
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/gateway/param/GatewayParamBuilder.java
  6. 9
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/BaseParamBuilder.java
  7. 2
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/param/ParamNames.java
  8. 5
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/result/BaseExecutorAdapter.java
  9. 5
      sop-common/sop-gateway-common/src/main/java/com/gitee/sop/gatewaycommon/zuul/param/ZuulParamBuilder.java
  10. 2
      sop-common/sop-registry-api/pom.xml
  11. 4
      sop-common/sop-service-common/pom.xml
  12. 2
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/bean/ParamNames.java
  13. 10
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/configuration/AlipayServiceConfiguration.java
  14. 14
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/mapping/ApiMappingRequestCondition.java
  15. 24
      sop-common/sop-service-common/src/main/java/com/gitee/sop/servercommon/param/ApiArgumentResolver.java
  16. 2
      sop-example/sop-auth/pom.xml
  17. 2
      sop-example/sop-auth/src/test/java/com/gitee/sop/sopauth/RefreshTokenTest.java
  18. 2
      sop-example/sop-book/sop-book-web/pom.xml
  19. 2
      sop-example/sop-easyopen/pom.xml
  20. 2
      sop-example/sop-springmvc/pom.xml
  21. 2
      sop-example/sop-story/sop-story-web/pom.xml
  22. 4
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/AlipayController.java
  23. 23
      sop-example/sop-story/sop-story-web/src/main/java/com/gitee/sop/storyweb/controller/PostJsonController.java
  24. 2
      sop-gateway/pom.xml
  25. 29
      sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenHttp.java
  26. 12
      sop-test/src/main/java/com/gitee/sop/test/HttpTool.java
  27. 31
      sop-test/src/test/java/com/gitee/sop/test/AlipayClientPostTest.java
  28. 36
      sop-test/src/test/java/com/gitee/sop/test/PostJsonTest.java
  29. 2
      sop-website/website-server/pom.xml
  30. 15
      sop-website/website-server/src/main/java/com/gitee/sop/websiteserver/bean/HttpTool.java

@ -1,5 +1,13 @@
# changelog # changelog
## 1.13.6
- 修复@RequestBody不能绑定问题
## 1.13.5
- 修复postJson下version获取不到问题
## 1.13.4 ## 1.13.4
- 修复admin服务列表最后更新时间不显示问题 - 修复admin服务列表最后更新时间不显示问题

@ -30,7 +30,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-registry-api</artifactId> <artifactId>sop-registry-api</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
</dependency> </dependency>
<!-- easyopen starter --> <!-- easyopen starter -->

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-common</artifactId> <artifactId>sop-common</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<properties> <properties>

@ -5,11 +5,11 @@
<parent> <parent>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-common</artifactId> <artifactId>sop-common</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<artifactId>sop-gateway-common</artifactId> <artifactId>sop-gateway-common</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>sop-gateway-common</name> <name>sop-gateway-common</name>

@ -22,4 +22,9 @@ public class GatewayParamBuilder extends BaseParamBuilder<ServerWebExchange> {
public String getIP(ServerWebExchange ctx) { public String getIP(ServerWebExchange ctx) {
return ctx.getRequest().getRemoteAddress().getAddress().getHostAddress(); return ctx.getRequest().getRemoteAddress().getAddress().getHostAddress();
} }
@Override
public void setVersionInHeader(ServerWebExchange ctx, String headerName, String version) {
ctx.getRequest().getHeaders().add(headerName, version);
}
} }

@ -31,6 +31,14 @@ public abstract class BaseParamBuilder<T> implements ParamBuilder<T> {
*/ */
public abstract String getIP(T ctx); public abstract String getIP(T ctx);
/**
* 将版本号添加到header中
* @param ctx 请求request
* @param headerName headerName
* @param version 版本号
*/
public abstract void setVersionInHeader(T ctx, String headerName, String version);
@Override @Override
public ApiParam build(T ctx) { public ApiParam build(T ctx) {
ApiParam apiParam = this.newApiParam(ctx); ApiParam apiParam = this.newApiParam(ctx);
@ -40,6 +48,7 @@ public abstract class BaseParamBuilder<T> implements ParamBuilder<T> {
} }
this.initOtherProperty(apiParam); this.initOtherProperty(apiParam);
apiParam.setIp(this.getIP(ctx)); apiParam.setIp(this.getIP(ctx));
this.setVersionInHeader(ctx, ParamNames.HEADER_VERSION_NAME, apiParam.fetchVersion());
return apiParam; return apiParam;
} }

@ -45,5 +45,7 @@ public class ParamNames {
/** 返回sign名称 */ /** 返回sign名称 */
public static String RESPONSE_SIGN_NAME = "sign"; public static String RESPONSE_SIGN_NAME = "sign";
public static String HEADER_VERSION_NAME = "sop-version";
} }

@ -37,6 +37,7 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
private static final ErrorMeta SUCCESS_META = ErrorEnum.SUCCESS.getErrorMeta(); private static final ErrorMeta SUCCESS_META = ErrorEnum.SUCCESS.getErrorMeta();
private static final ErrorMeta ISP_UNKNOW_ERROR_META = ErrorEnum.ISP_UNKNOW_ERROR.getErrorMeta(); private static final ErrorMeta ISP_UNKNOW_ERROR_META = ErrorEnum.ISP_UNKNOW_ERROR.getErrorMeta();
private static final ErrorMeta ISP_BIZ_ERROR = ErrorEnum.BIZ_ERROR.getErrorMeta(); private static final ErrorMeta ISP_BIZ_ERROR = ErrorEnum.BIZ_ERROR.getErrorMeta();
private static final ErrorMeta ISV_MISSING_METHOD_META = ErrorEnum.ISV_MISSING_METHOD.getErrorMeta();
public static final String GATEWAY_CODE_NAME = "code"; public static final String GATEWAY_CODE_NAME = "code";
public static final String GATEWAY_MSG_NAME = "msg"; public static final String GATEWAY_MSG_NAME = "msg";
@ -90,6 +91,10 @@ public abstract class BaseExecutorAdapter<T, R> implements ResultExecutor<T, R>
responseData = JSON.parseObject(serviceResult); responseData = JSON.parseObject(serviceResult);
responseData.put(GATEWAY_CODE_NAME, ISP_BIZ_ERROR.getCode()); responseData.put(GATEWAY_CODE_NAME, ISP_BIZ_ERROR.getCode());
responseData.put(GATEWAY_MSG_NAME, ISP_BIZ_ERROR.getError().getMsg()); responseData.put(GATEWAY_MSG_NAME, ISP_BIZ_ERROR.getError().getMsg());
} else if(responseStatus == HttpStatus.NOT_FOUND.value()) {
responseData = JSON.parseObject(serviceResult);
responseData.put(GATEWAY_CODE_NAME, ISV_MISSING_METHOD_META.getCode());
responseData.put(GATEWAY_MSG_NAME, ISV_MISSING_METHOD_META.getError().getCode());
} else { } else {
this.storeError(request, ErrorType.UNKNOWN); this.storeError(request, ErrorType.UNKNOWN);
// 微服务端有可能返回500错误 // 微服务端有可能返回500错误

@ -53,6 +53,11 @@ public class ZuulParamBuilder extends BaseParamBuilder<RequestContext> {
return RequestUtil.getIP(ctx.getRequest()); return RequestUtil.getIP(ctx.getRequest());
} }
@Override
public void setVersionInHeader(RequestContext ctx, String headerName, String version) {
ctx.addZuulRequestHeader(headerName, version);
}
@Override @Override
protected ApiParam newApiParam(RequestContext ctx) { protected ApiParam newApiParam(RequestContext ctx) {
ApiParam apiParam = super.newApiParam(ctx); ApiParam apiParam = super.newApiParam(ctx);

@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-registry-api</artifactId> <artifactId>sop-registry-api</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

@ -6,11 +6,11 @@
<parent> <parent>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-common</artifactId> <artifactId>sop-common</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>sop-service-common</name> <name>sop-service-common</name>

@ -45,5 +45,7 @@ public class ParamNames {
/** 时间戳格式 */ /** 时间戳格式 */
public static String TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss"; public static String TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static String HEADER_VERSION_NAME = "sop-version";
} }

@ -3,11 +3,8 @@ package com.gitee.sop.servercommon.configuration;
import com.gitee.sop.servercommon.bean.ServiceConfig; import com.gitee.sop.servercommon.bean.ServiceConfig;
import com.gitee.sop.servercommon.param.SopHandlerMethodArgumentResolver; import com.gitee.sop.servercommon.param.SopHandlerMethodArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import java.util.List;
/** /**
* 具备支付宝开放平台服务提供能力 * 具备支付宝开放平台服务提供能力
* @author tanghc * @author tanghc
@ -22,13 +19,6 @@ public class AlipayServiceConfiguration extends BaseServiceConfiguration {
@Autowired @Autowired
private RequestMappingHandlerAdapter requestMappingHandlerAdapter; private RequestMappingHandlerAdapter requestMappingHandlerAdapter;
@Override
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
super.addArgumentResolvers(argumentResolvers);
SopHandlerMethodArgumentResolver sopHandlerMethodArgumentResolver = ServiceConfig.getInstance().getMethodArgumentResolver();
argumentResolvers.add(sopHandlerMethodArgumentResolver);
}
@Override @Override
protected void doAfter() { protected void doAfter() {
super.doAfter(); super.doAfter();

@ -15,7 +15,6 @@ import javax.servlet.http.HttpServletRequest;
@Slf4j @Slf4j
public class ApiMappingRequestCondition implements RequestCondition<ApiMappingRequestCondition> { public class ApiMappingRequestCondition implements RequestCondition<ApiMappingRequestCondition> {
public static final String UTF_8 = "UTF-8";
private String defaultVersion = ServiceConfig.getInstance().getDefaultVersion(); private String defaultVersion = ServiceConfig.getInstance().getDefaultVersion();
private ApiMappingInfo apiMappingInfo; private ApiMappingInfo apiMappingInfo;
@ -46,7 +45,10 @@ public class ApiMappingRequestCondition implements RequestCondition<ApiMappingRe
} }
protected String getVersion(HttpServletRequest request) { protected String getVersion(HttpServletRequest request) {
String version = request.getParameter(ParamNames.VERSION_NAME); String version = request.getHeader(ParamNames.HEADER_VERSION_NAME);
if (version == null) {
version = request.getParameter(ParamNames.VERSION_NAME);
}
return version == null ? defaultVersion : version; return version == null ? defaultVersion : version;
} }
@ -61,13 +63,7 @@ public class ApiMappingRequestCondition implements RequestCondition<ApiMappingRe
*/ */
@Override @Override
public int compareTo(ApiMappingRequestCondition other, HttpServletRequest request) { public int compareTo(ApiMappingRequestCondition other, HttpServletRequest request) {
if (null != apiMappingInfo && null == other.apiMappingInfo) { return this.apiMappingInfo.getVersion().compareTo(other.apiMappingInfo.getVersion());
return 1;
} else if (null == apiMappingInfo && null != other.apiMappingInfo) {
return -1;
} else {
return 0;
}
} }
} }

@ -17,12 +17,15 @@ import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
/** /**
@ -42,14 +45,29 @@ public class ApiArgumentResolver implements SopHandlerMethodArgumentResolver {
@Override @Override
public void setRequestMappingHandlerAdapter(RequestMappingHandlerAdapter requestMappingHandlerAdapter) { public void setRequestMappingHandlerAdapter(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
this.requestMappingHandlerAdapter = requestMappingHandlerAdapter; this.requestMappingHandlerAdapter = requestMappingHandlerAdapter;
List<HandlerMethodArgumentResolver> argumentResolversNew = new ArrayList<>(64);
// 先加自己
argumentResolversNew.add(this);
HandlerMethodArgumentResolver lastOne = null;
for (HandlerMethodArgumentResolver argumentResolver : Objects.requireNonNull(requestMappingHandlerAdapter.getArgumentResolvers())) {
// RequestResponseBodyMethodProcessor暂存起来,放在最后面
if (argumentResolver instanceof RequestResponseBodyMethodProcessor) {
lastOne = argumentResolver;
} else {
argumentResolversNew.add(argumentResolver);
}
}
if (lastOne != null) {
argumentResolversNew.add(lastOne);
}
this.requestMappingHandlerAdapter.setArgumentResolvers(argumentResolversNew);
} }
@Override @Override
public boolean supportsParameter(MethodParameter methodParameter) { public boolean supportsParameter(MethodParameter methodParameter) {
boolean hasAnnotation = methodParameter.getMethodAnnotation(ApiMapping.class) != null
|| methodParameter.getMethodAnnotation(ApiAbility.class) != null;
// 有注解 // 有注解
return hasAnnotation; return methodParameter.getMethodAnnotation(ApiMapping.class) != null
|| methodParameter.getMethodAnnotation(ApiAbility.class) != null;
} }
@Override @Override

@ -26,7 +26,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>

@ -26,7 +26,7 @@ public class RefreshTokenTest extends TestCase {
OpenAuthTokenAppRequest request = new OpenAuthTokenAppRequest(); OpenAuthTokenAppRequest request = new OpenAuthTokenAppRequest();
OpenAuthTokenAppModel model = new OpenAuthTokenAppModel(); OpenAuthTokenAppModel model = new OpenAuthTokenAppModel();
model.setGrant_type("refresh_token"); model.setGrant_type("refresh_token");
model.setRefresh_token("856faf8d77d3b985c1073557ce6ea724"); model.setRefresh_token("c9e4003c06fe59066eed73d64ea074ca");
request.setBizModel(model); request.setBizModel(model);
OpenAuthTokenAppResponse response = openClient.execute(request); OpenAuthTokenAppResponse response = openClient.execute(request);

@ -24,7 +24,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>

@ -29,7 +29,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>

@ -20,7 +20,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
</dependency> </dependency>
<!-- eureka 服务发现 --> <!-- eureka 服务发现 -->
<dependency> <dependency>

@ -24,7 +24,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-service-common</artifactId> <artifactId>sop-service-common</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>

@ -30,7 +30,7 @@ public class AlipayController {
// http://localhost:2222/story_get // http://localhost:2222/story_get
// 原生的接口,可正常调用 // 原生的接口,可正常调用
@RequestMapping("story_get") @RequestMapping("story_get")
public Story getStory4() { public Story story_get() {
Story story = new Story(); Story story = new Story();
story.setId(1); story.setId(1);
story.setName("海底小纵队(原生)"); story.setName("海底小纵队(原生)");
@ -117,7 +117,7 @@ public class AlipayController {
// http://localhost:2222/getStory2?version=2.1 // http://localhost:2222/getStory2?version=2.1
// 遗留接口具备开放平台能力,在原来的基础上加版本号 // 遗留接口具备开放平台能力,在原来的基础上加版本号
@ApiAbility(version = "2.1") @ApiAbility(version = "2.1")
@RequestMapping("getStory2") @GetMapping("getStory2")
public Story getStory2_1() { public Story getStory2_1() {
Story story = new Story(); Story story = new Story();
story.setId(1); story.setId(1);

@ -6,6 +6,7 @@ import com.gitee.sop.servercommon.bean.OpenContext;
import com.gitee.sop.story.api.domain.Story; import com.gitee.sop.story.api.domain.Story;
import com.gitee.sop.storyweb.controller.param.StoryParam; import com.gitee.sop.storyweb.controller.param.StoryParam;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays; import java.util.Arrays;
@ -23,7 +24,7 @@ public class PostJsonController {
* @return * @return
*/ */
@ApiMapping("demo.post.json") @ApiMapping("demo.post.json")
public Story postJson(StoryParam param) { public Story postJson(@RequestBody StoryParam param) {
// 获取开放平台请求参数 // 获取开放平台请求参数
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext(); OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
List<Object> params = Arrays.asList( List<Object> params = Arrays.asList(
@ -37,5 +38,25 @@ public class PostJsonController {
return story; return story;
} }
/**
* 演示客户端使用json方式请求application/json
* @param param
* @return
*/
@ApiMapping(value = "demo.post.json", version = "1.2")
public Story postJson2(StoryParam param) {
// 获取开放平台请求参数
OpenContext openContext = ServiceContext.getCurrentContext().getOpenContext();
List<Object> params = Arrays.asList(
openContext.getAppId(),
openContext.getMethod(),
openContext.getVersion()
);
Story story = new Story();
story.setId(1);
story.setName("1.2 参数:" + param.getName() + ", openParams:" + StringUtils.join(params));
return story;
}
} }

@ -23,7 +23,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-gateway-common</artifactId> <artifactId>sop-gateway-common</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
</dependency> </dependency>
<!-- ↓↓↓ 使用spring cloud zuul ↓↓↓ --> <!-- ↓↓↓ 使用spring cloud zuul ↓↓↓ -->

@ -118,7 +118,7 @@ public class OpenHttp {
* @return * @return
* @throws IOException * @throws IOException
*/ */
public String request(String url, Map<String, String> form, Map<String, String> header, String method) throws IOException { public String request(String url, Map<String, String> form, Map<String, String> header, HTTPMethod method) throws IOException {
Request.Builder requestBuilder = buildRequestBuilder(url, form, method); Request.Builder requestBuilder = buildRequestBuilder(url, form, method);
// 添加header // 添加header
addHeader(requestBuilder, header); addHeader(requestBuilder, header);
@ -134,21 +134,21 @@ public class OpenHttp {
} }
} }
public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, String method) { public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, HTTPMethod method) {
switch (method) { switch (method) {
case "get": case GET:
return new Request.Builder() return new Request.Builder()
.url(buildHttpUrl(url, form)) .url(buildHttpUrl(url, form))
.get(); .get();
case "head": case HEAD:
return new Request.Builder() return new Request.Builder()
.url(buildHttpUrl(url, form)) .url(buildHttpUrl(url, form))
.head(); .head();
case "put": case PUT:
return new Request.Builder() return new Request.Builder()
.url(url) .url(url)
.put(buildFormBody(form)); .put(buildFormBody(form));
case "delete": case DELETE:
return new Request.Builder() return new Request.Builder()
.url(url) .url(url)
.delete(buildFormBody(form)); .delete(buildFormBody(form));
@ -241,5 +241,22 @@ public class OpenHttp {
this.httpClient = httpClient; this.httpClient = httpClient;
} }
public enum HTTPMethod {
GET,
POST,
PUT,
HEAD,
DELETE;
private HTTPMethod() {
}
public String value() {
return this.name();
}
public static HTTPMethod fromValue(String v) {
return valueOf(v.toUpperCase());
}
}
} }

@ -111,7 +111,7 @@ public class HttpTool {
* @throws IOException * @throws IOException
*/ */
public String request(String url, Map<String, String> form, Map<String, String> header, HTTPMethod method) throws IOException { public String request(String url, Map<String, String> form, Map<String, String> header, HTTPMethod method) throws IOException {
Request.Builder requestBuilder = buildRequestBuilder(url, form, method.value()); Request.Builder requestBuilder = buildRequestBuilder(url, form, method);
// 添加header // 添加header
addHeader(requestBuilder, header); addHeader(requestBuilder, header);
@ -153,21 +153,21 @@ public class HttpTool {
} }
} }
public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, String method) { public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, HTTPMethod method) {
switch (method) { switch (method) {
case "get": case GET:
return new Request.Builder() return new Request.Builder()
.url(buildHttpUrl(url, form)) .url(buildHttpUrl(url, form))
.get(); .get();
case "head": case HEAD:
return new Request.Builder() return new Request.Builder()
.url(buildHttpUrl(url, form)) .url(buildHttpUrl(url, form))
.head(); .head();
case "put": case PUT:
return new Request.Builder() return new Request.Builder()
.url(url) .url(url)
.put(buildFormBody(form)); .put(buildFormBody(form));
case "delete": case DELETE:
return new Request.Builder() return new Request.Builder()
.url(url) .url(url)
.delete(buildFormBody(form)); .delete(buildFormBody(form));

@ -231,4 +231,35 @@ public class AlipayClientPostTest extends TestBase {
System.out.println(responseData); System.out.println(responseData);
} }
@Test
public void testGetStory2() throws AlipayApiException {
// 公共请求参数
Map<String, String> params = new HashMap<String, String>();
params.put("app_id", appId);
params.put("method", "getStory2");
params.put("version", "1.0");
params.put("format", "json");
params.put("charset", "utf-8");
params.put("sign_type", "RSA2");
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
// 业务参数
Map<String, String> bizContent = new HashMap<>();
params.put("biz_content", JSON.toJSONString(bizContent));
System.out.println("----------- 请求信息 -----------");
System.out.println("请求参数:" + buildParamQuery(params));
System.out.println("商户秘钥:" + privateKey);
String content = AlipaySignature.getSignContent(params);
System.out.println("待签名内容:" + content);
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
System.out.println("签名(sign):" + sign);
params.put("sign", sign);
System.out.println("----------- 返回结果 -----------");
String responseData = get(url, params);// 发送请求
System.out.println(responseData);
}
} }

@ -49,7 +49,41 @@ public class PostJsonTest extends TestBase {
params.put("sign", sign); params.put("sign", sign);
System.out.println("----------- 返回结果 -----------"); System.out.println("----------- 返回结果 -----------");
String responseData = get(url, params);// 发送请求 String responseData = postJson(url, params);// 发送请求
System.out.println(responseData);
}
@Test
public void testPostJson2() throws Exception {
// 公共请求参数
Map<String, String> params = new HashMap<String, String>();
params.put("app_id", appId);
params.put("method", "demo.post.json");
params.put("format", "json");
params.put("charset", "utf-8");
params.put("sign_type", "RSA2");
params.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
params.put("version", "1.2");
// 业务参数
Map<String, String> bizContent = new HashMap<>();
bizContent.put("name", "葫芦娃");
params.put("biz_content", JSON.toJSONString(bizContent));
System.out.println("----------- 请求信息 -----------");
System.out.println("请求参数:" + buildParamQuery(params));
System.out.println("商户秘钥:" + privateKey);
String content = AlipaySignature.getSignContent(params);
System.out.println("待签名内容:" + content);
String sign = AlipaySignature.rsa256Sign(content, privateKey, "utf-8");
System.out.println("签名(sign):" + sign);
params.put("sign", sign);
System.out.println("----------- 返回结果 -----------");
String responseData = postJson(url, params);// 发送请求
System.out.println(responseData); System.out.println(responseData);
} }

@ -25,7 +25,7 @@
<dependency> <dependency>
<groupId>com.gitee.sop</groupId> <groupId>com.gitee.sop</groupId>
<artifactId>sop-registry-api</artifactId> <artifactId>sop-registry-api</artifactId>
<version>1.13.4-SNAPSHOT</version> <version>1.13.6-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>

@ -111,7 +111,7 @@ public class HttpTool {
* @throws IOException * @throws IOException
*/ */
public String request(String url, Map<String, String> form, Map<String, String> header, HTTPMethod method) throws IOException { public String request(String url, Map<String, String> form, Map<String, String> header, HTTPMethod method) throws IOException {
Request.Builder requestBuilder = buildRequestBuilder(url, form, method.value()); Request.Builder requestBuilder = buildRequestBuilder(url, form, method);
// 添加header // 添加header
addHeader(requestBuilder, header); addHeader(requestBuilder, header);
@ -153,21 +153,21 @@ public class HttpTool {
} }
} }
public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, String method) { public static Request.Builder buildRequestBuilder(String url, Map<String, String> form, HTTPMethod method) {
switch (method) { switch (method) {
case "get": case GET:
return new Request.Builder() return new Request.Builder()
.url(buildHttpUrl(url, form)) .url(buildHttpUrl(url, form))
.get(); .get();
case "head": case HEAD:
return new Request.Builder() return new Request.Builder()
.url(buildHttpUrl(url, form)) .url(buildHttpUrl(url, form))
.head(); .head();
case "put": case PUT:
return new Request.Builder() return new Request.Builder()
.url(url) .url(url)
.put(buildFormBody(form)); .put(buildFormBody(form));
case "delete": case DELETE:
return new Request.Builder() return new Request.Builder()
.url(url) .url(url)
.delete(buildFormBody(form)); .delete(buildFormBody(form));
@ -262,6 +262,9 @@ public class HttpTool {
HEAD, HEAD,
DELETE; DELETE;
private HTTPMethod() {
}
public String value() { public String value() {
return this.name(); return this.name();
} }

Loading…
Cancel
Save