From 56ed5c8d720586d360806d7b22bcf9c148803b09 Mon Sep 17 00:00:00 2001 From: jinyu Date: Fri, 17 Mar 2017 09:51:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96HttpClientFatory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../weixin4j/http/SimpleHttpClient.java | 5 -- .../http/factory/HttpClientFactory.java | 30 ++------- .../http/factory/SimpleHttpClientFactory.java | 9 +-- .../apache3/HttpComponent3Factory.java | 43 ++++++------- .../apache4/HttpComponent4Factory.java | 9 +-- .../apache4/HttpComponent4_1Factory.java | 61 ++++++++++--------- .../apache4/HttpComponent4_2Factory.java | 57 +++++++++-------- .../netty/Netty4HttpClientFactory.java | 12 +--- .../support/okhttp/OkHttpClient2Factory.java | 33 +++++----- .../support/okhttp/OkHttpClient3Factory.java | 36 +++++------ .../support/okhttp/OkHttpClientFactory.java | 9 +-- .../weixin4j/jssdk/JSSDKConfigurator.java | 4 +- .../base/test/http/HttpClientTest.java | 6 +- .../example/server/Weixin4jServerStartup.java | 3 +- .../server/handler/TextMessageHandler.java | 1 - 16 files changed, 135 insertions(+), 184 deletions(-) diff --git a/.gitignore b/.gitignore index 87bd9366..1d3be1be 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,5 @@ target/* *.tmp Thumbs.db /target/ +weixin4j-future .DS_Store diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/SimpleHttpClient.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/SimpleHttpClient.java index 6d78e3a6..9bf42824 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/SimpleHttpClient.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/SimpleHttpClient.java @@ -71,11 +71,6 @@ public class SimpleHttpClient extends AbstractHttpClient { // create connection object HttpURLConnection connection = createHttpConnection(request); String method = request.getMethod().name(); - // set parameters - if (params != null) { - connection.setConnectTimeout(params.getConnectTimeout()); - connection.setReadTimeout(params.getReadTimeout()); - } connection.setRequestMethod(method); connection.setDoInput(true); connection.setInstanceFollowRedirects("GET".equals(method)); diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/factory/HttpClientFactory.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/factory/HttpClientFactory.java index 6abe87e2..a77bf478 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/factory/HttpClientFactory.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/factory/HttpClientFactory.java @@ -125,39 +125,17 @@ public abstract class HttpClientFactory { */ public static HttpClient getInstance(HttpParams params) { HttpClientFactory clientFactory = getDefaultFactory(); - if (params != null) { - clientFactory.resolveHttpParams(params); - } - return clientFactory.newInstance(); + return clientFactory.newInstance(params); } - /** - * Resolve the Http Parameter - * - * @param params - * 请求参数 - */ - public void resolveHttpParams(HttpParams params){ - if (params == null) { - throw new IllegalArgumentException("'params' must not be empty"); - } - resolveHttpParams0(params); - } - - /** - * Resolve the Http Parameter - * - * @param params - * 请求参数 - */ - protected abstract void resolveHttpParams0(HttpParams params); - /** * 获取HttpClient实例 * + * @param params + * http参数 可为空 * @return */ - public abstract HttpClient newInstance(); + public abstract HttpClient newInstance(HttpParams params); public static SSLContext allowSSLContext() { try { diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/factory/SimpleHttpClientFactory.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/factory/SimpleHttpClientFactory.java index 802ba858..c73cc50f 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/factory/SimpleHttpClientFactory.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/factory/SimpleHttpClientFactory.java @@ -15,15 +15,8 @@ import com.foxinmy.weixin4j.http.SimpleHttpClient; */ public class SimpleHttpClientFactory extends HttpClientFactory { - private HttpParams params; - @Override - protected void resolveHttpParams0(HttpParams params) { - this.params = params; - } - - @Override - public HttpClient newInstance() { + public HttpClient newInstance(HttpParams params) { return new SimpleHttpClient(params); } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache3/HttpComponent3Factory.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache3/HttpComponent3Factory.java index 97a57b74..4ee8234f 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache3/HttpComponent3Factory.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache3/HttpComponent3Factory.java @@ -4,7 +4,6 @@ import java.net.InetSocketAddress; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpConnectionManager; -import org.apache.commons.httpclient.params.HttpClientParams; import org.apache.commons.httpclient.protocol.Protocol; import com.foxinmy.weixin4j.http.HttpParams; @@ -40,25 +39,6 @@ public class HttpComponent3Factory extends HttpClientFactory { this.httpClient = httpClient; } - /** - * Resolve Parameter - */ - @Override - protected void resolveHttpParams0(HttpParams params) { - if (params.getProxy() != null) { - InetSocketAddress socketAddress = (InetSocketAddress) params - .getProxy().address(); - httpClient.getHostConfiguration().setProxy( - socketAddress.getHostName(), socketAddress.getPort()); - } - if (params.getSSLContext() != null) { - Protocol.registerProtocol("https", new Protocol("https", - new SSLProtocolSocketFactory(params.getSSLContext()), 443)); - } - httpClient.getHttpConnectionManager().getParams() - .setConnectionTimeout(params.getConnectTimeout()); - } - public void setHttpConnectionManager( HttpConnectionManager httpConnectionManager) { if (httpConnectionManager == null) { @@ -68,16 +48,27 @@ public class HttpComponent3Factory extends HttpClientFactory { httpClient.setHttpConnectionManager(httpConnectionManager); } - public void setHttpClientParams(HttpClientParams httpClientParams) { - if (httpClientParams == null) { - throw new IllegalArgumentException( - "'httpClientParams' must not be null"); + private void resolveHttpParams(HttpParams params) { + if (params != null) { + if (params.getProxy() != null) { + InetSocketAddress socketAddress = (InetSocketAddress) params + .getProxy().address(); + httpClient.getHostConfiguration().setProxy( + socketAddress.getHostName(), socketAddress.getPort()); + } + if (params.getSSLContext() != null) { + Protocol.registerProtocol("https", new Protocol("https", + new SSLProtocolSocketFactory(params.getSSLContext()), + 443)); + } + httpClient.getHttpConnectionManager().getParams() + .setConnectionTimeout(params.getConnectTimeout()); } - httpClient.setParams(httpClientParams); } @Override - public com.foxinmy.weixin4j.http.HttpClient newInstance() { + public com.foxinmy.weixin4j.http.HttpClient newInstance(HttpParams params) { + resolveHttpParams(params); return new HttpComponent3(httpClient); } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4Factory.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4Factory.java index f76e6147..475333a8 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4Factory.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4Factory.java @@ -36,12 +36,7 @@ public class HttpComponent4Factory extends HttpClientFactory { } @Override - public HttpClient newInstance() { - return httpComponentFactory.newInstance(); - } - - @Override - protected void resolveHttpParams0(HttpParams params) { - httpComponentFactory.resolveHttpParams(params); + public HttpClient newInstance(HttpParams params) { + return httpComponentFactory.newInstance(params); } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_1Factory.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_1Factory.java index 45cb43ba..bad711bd 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_1Factory.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_1Factory.java @@ -70,34 +70,6 @@ public class HttpComponent4_1Factory extends HttpClientFactory { this.httpClient = httpClient; } - @Override - protected void resolveHttpParams0(HttpParams params) { - if (params.getProxy() != null) { - InetSocketAddress socketAddress = (InetSocketAddress) params - .getProxy().address(); - HttpHost proxy = new HttpHost(socketAddress.getHostName(), - socketAddress.getPort()); - httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, - proxy); - } - httpClient.getParams().setIntParameter( - CoreConnectionPNames.CONNECTION_TIMEOUT, - params.getConnectTimeout()); - httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, - params.getReadTimeout()); - if (params.getSSLContext() != null) { - SSLSocketFactory socketFactory = new SSLSocketFactory( - params.getSSLContext()); - if (params.getHostnameVerifier() != null) { - socketFactory.setHostnameVerifier(new CustomHostnameVerifier( - params.getHostnameVerifier())); - } - Scheme scheme = new Scheme("https", socketFactory, 443); - httpClient.getConnectionManager().getSchemeRegistry() - .register(scheme); - } - } - /** * 设置Http参数 * @@ -111,8 +83,39 @@ public class HttpComponent4_1Factory extends HttpClientFactory { httpClient.getParams().setParameter(name, value); } + private void resolveHttpParams(HttpParams params) { + if (params != null) { + if (params.getProxy() != null) { + InetSocketAddress socketAddress = (InetSocketAddress) params + .getProxy().address(); + HttpHost proxy = new HttpHost(socketAddress.getHostName(), + socketAddress.getPort()); + httpClient.getParams().setParameter( + ConnRoutePNames.DEFAULT_PROXY, proxy); + } + httpClient.getParams().setIntParameter( + CoreConnectionPNames.CONNECTION_TIMEOUT, + params.getConnectTimeout()); + httpClient.getParams().setIntParameter( + CoreConnectionPNames.SO_TIMEOUT, params.getReadTimeout()); + if (params.getSSLContext() != null) { + SSLSocketFactory socketFactory = new SSLSocketFactory( + params.getSSLContext()); + if (params.getHostnameVerifier() != null) { + socketFactory + .setHostnameVerifier(new CustomHostnameVerifier( + params.getHostnameVerifier())); + } + Scheme scheme = new Scheme("https", socketFactory, 443); + httpClient.getConnectionManager().getSchemeRegistry() + .register(scheme); + } + } + } + @Override - public HttpClient newInstance() { + public HttpClient newInstance(HttpParams params) { + resolveHttpParams(params); return new HttpComponent4_1(httpClient); } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_2Factory.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_2Factory.java index 86909fe9..d313df63 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_2Factory.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/apache4/HttpComponent4_2Factory.java @@ -45,32 +45,6 @@ public class HttpComponent4_2Factory extends HttpClientFactory { this.clientBuilder = clientBuilder; } - @Override - protected void resolveHttpParams0(HttpParams params) { - clientBuilder.setDefaultRequestConfig(RequestConfig.custom() - .setConnectTimeout(params.getConnectTimeout()) - .setConnectionRequestTimeout(params.getReadTimeout()).build()); - if (params.getProxy() != null) { - InetSocketAddress socketAddress = (InetSocketAddress) params - .getProxy().address(); - HttpHost proxy = new HttpHost(socketAddress.getHostName(), - socketAddress.getPort()); - clientBuilder.setProxy(proxy); - } - if (params.getSSLContext() != null) { - clientBuilder - .setSSLSocketFactory(new SSLConnectionSocketFactory( - params.getSSLContext(), - params.getHostnameVerifier() != null ? new CustomHostnameVerifier( - params.getHostnameVerifier()) - : SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)); - } - if (params.getHostnameVerifier() != null) { - clientBuilder.setHostnameVerifier(new CustomHostnameVerifier(params - .getHostnameVerifier())); - } - } - public HttpComponent4_2Factory setDefaultConnectionConfig( ConnectionConfig connectionConfig) { clientBuilder.setDefaultConnectionConfig(connectionConfig); @@ -100,8 +74,37 @@ public class HttpComponent4_2Factory extends HttpClientFactory { return this; } + private void resolveHttpParams(HttpParams params) { + if (params != null) { + clientBuilder.setDefaultRequestConfig(RequestConfig.custom() + .setConnectTimeout(params.getConnectTimeout()) + .setConnectionRequestTimeout(params.getReadTimeout()) + .build()); + if (params.getProxy() != null) { + InetSocketAddress socketAddress = (InetSocketAddress) params + .getProxy().address(); + HttpHost proxy = new HttpHost(socketAddress.getHostName(), + socketAddress.getPort()); + clientBuilder.setProxy(proxy); + } + if (params.getSSLContext() != null) { + clientBuilder + .setSSLSocketFactory(new SSLConnectionSocketFactory( + params.getSSLContext(), + params.getHostnameVerifier() != null ? new CustomHostnameVerifier( + params.getHostnameVerifier()) + : SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)); + } + if (params.getHostnameVerifier() != null) { + clientBuilder.setHostnameVerifier(new CustomHostnameVerifier( + params.getHostnameVerifier())); + } + } + } + @Override - public HttpClient newInstance() { + public HttpClient newInstance(HttpParams params) { + resolveHttpParams(params); return new HttpComponent4_2(clientBuilder.build()); } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/netty/Netty4HttpClientFactory.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/netty/Netty4HttpClientFactory.java index 49fa8cd8..194c90e1 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/netty/Netty4HttpClientFactory.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/netty/Netty4HttpClientFactory.java @@ -34,7 +34,6 @@ public class Netty4HttpClientFactory extends HttpClientFactory { private volatile Bootstrap bootstrap; private EventLoopGroup eventLoopGroup; private Map, ?> options; - private HttpParams params; public Netty4HttpClientFactory() { this(new NioEventLoopGroup( @@ -45,11 +44,6 @@ public class Netty4HttpClientFactory extends HttpClientFactory { this.eventLoopGroup = eventLoopGroup; } - @Override - protected void resolveHttpParams0(HttpParams params) { - this.params = params; - } - public Netty4HttpClientFactory setOptions(Map, ?> options) { if (options == null) { throw new IllegalArgumentException("'options' must not be empty"); @@ -58,7 +52,7 @@ public class Netty4HttpClientFactory extends HttpClientFactory { return this; } - private Bootstrap getBootstrap() { + private Bootstrap getBootstrap(final HttpParams params) { if (bootstrap == null) { bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class) @@ -90,7 +84,7 @@ public class Netty4HttpClientFactory extends HttpClientFactory { } @Override - public HttpClient newInstance() { - return new Netty4HttpClient(getBootstrap(), params); + public HttpClient newInstance(HttpParams params) { + return new Netty4HttpClient(getBootstrap(params), params); } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClient2Factory.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClient2Factory.java index 5f4057d1..a70419c4 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClient2Factory.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClient2Factory.java @@ -35,20 +35,22 @@ public class OkHttpClient2Factory extends HttpClientFactory { this.okClient = okClient; } - @Override - protected void resolveHttpParams0(HttpParams params) { - okClient.setConnectTimeout(params.getConnectTimeout(), - TimeUnit.MILLISECONDS); - okClient.setReadTimeout(params.getReadTimeout(), TimeUnit.MILLISECONDS); - if (params.getProxy() != null) { - okClient.setProxy(params.getProxy()); - } - if (params.getSSLContext() != null) { - okClient.setSslSocketFactory(params.getSSLContext() - .getSocketFactory()); - } - if (params.getHostnameVerifier() != null) { - okClient.setHostnameVerifier(params.getHostnameVerifier()); + private void resolveHttpParams(HttpParams params) { + if (params != null) { + okClient.setConnectTimeout(params.getConnectTimeout(), + TimeUnit.MILLISECONDS); + okClient.setReadTimeout(params.getReadTimeout(), + TimeUnit.MILLISECONDS); + if (params.getProxy() != null) { + okClient.setProxy(params.getProxy()); + } + if (params.getSSLContext() != null) { + okClient.setSslSocketFactory(params.getSSLContext() + .getSocketFactory()); + } + if (params.getHostnameVerifier() != null) { + okClient.setHostnameVerifier(params.getHostnameVerifier()); + } } } @@ -83,7 +85,8 @@ public class OkHttpClient2Factory extends HttpClientFactory { } @Override - public HttpClient newInstance() { + public HttpClient newInstance(HttpParams params) { + resolveHttpParams(params); return new OkHttpClient2(okClient); } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClient3Factory.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClient3Factory.java index 35ee0881..c54fd334 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClient3Factory.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClient3Factory.java @@ -43,22 +43,23 @@ public class OkHttpClient3Factory extends HttpClientFactory { * resolve Request.Parameter * * */ - @Override - protected void resolveHttpParams0(HttpParams params) { - clientBuilder.connectTimeout(params.getConnectTimeout(), - TimeUnit.MILLISECONDS); - clientBuilder.readTimeout(params.getReadTimeout(), - TimeUnit.MILLISECONDS); - if (params.getProxy() != null) { - clientBuilder.proxy(params.getProxy()); - } - if (params.getSSLContext() != null) { - clientBuilder.sslSocketFactory(params.getSSLContext() - .getSocketFactory(), - HttpClientFactory.AllowX509TrustManager.GLOBAL); - } - if (params.getHostnameVerifier() != null) { - clientBuilder.hostnameVerifier(params.getHostnameVerifier()); + private void resolveHttpParams(HttpParams params) { + if (params != null) { + clientBuilder.connectTimeout(params.getConnectTimeout(), + TimeUnit.MILLISECONDS); + clientBuilder.readTimeout(params.getReadTimeout(), + TimeUnit.MILLISECONDS); + if (params.getProxy() != null) { + clientBuilder.proxy(params.getProxy()); + } + if (params.getSSLContext() != null) { + clientBuilder.sslSocketFactory(params.getSSLContext() + .getSocketFactory(), + HttpClientFactory.AllowX509TrustManager.GLOBAL); + } + if (params.getHostnameVerifier() != null) { + clientBuilder.hostnameVerifier(params.getHostnameVerifier()); + } } } @@ -104,7 +105,8 @@ public class OkHttpClient3Factory extends HttpClientFactory { } @Override - public HttpClient newInstance() { + public HttpClient newInstance(HttpParams params) { + resolveHttpParams(params); if (okClient == null) { okClient = clientBuilder.build(); } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClientFactory.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClientFactory.java index 07e9d4de..2e84991a 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClientFactory.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/http/support/okhttp/OkHttpClientFactory.java @@ -27,12 +27,7 @@ public class OkHttpClientFactory extends HttpClientFactory { } @Override - protected void resolveHttpParams0(HttpParams params) { - okHttpClientFactory.resolveHttpParams(params); - } - - @Override - public HttpClient newInstance() { - return okHttpClientFactory.newInstance(); + public HttpClient newInstance(HttpParams params) { + return okHttpClientFactory.newInstance(params); } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/jssdk/JSSDKConfigurator.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/jssdk/JSSDKConfigurator.java index dfd70c43..c6531f48 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/jssdk/JSSDKConfigurator.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/jssdk/JSSDKConfigurator.java @@ -85,9 +85,7 @@ public class JSSDKConfigurator { */ public JSSDKConfigurator apis(JSSDKAPI[]... apis) { for (JSSDKAPI[] api : apis) { - for (JSSDKAPI apii : api) { - this.apis.add(apii); - } + apis(api); } return this; } diff --git a/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/http/HttpClientTest.java b/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/http/HttpClientTest.java index d9a15d9d..273ed1dc 100644 --- a/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/http/HttpClientTest.java +++ b/weixin4j-base/src/test/java/com/foxinmy/weixin4j/base/test/http/HttpClientTest.java @@ -34,7 +34,7 @@ public abstract class HttpClientTest { protected abstract HttpClientFactory createHttpFactory(); protected HttpClient createHttpClient() { - return createHttpFactory().newInstance(); + return createHttpFactory().newInstance(null); } protected HttpClient createProxyHttpClient() { @@ -60,8 +60,8 @@ public abstract class HttpClientTest { protected HttpClient createHttpClient(HttpParams params) { HttpClientFactory httpClientFactory = createHttpFactory(); - httpClientFactory.setDefaultParams(params); - return httpClientFactory.newInstance(); + HttpClientFactory.setDefaultParams(params); + return httpClientFactory.newInstance(null); } @Test diff --git a/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartup.java b/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartup.java index b2549c87..3ad8c5ef 100644 --- a/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartup.java +++ b/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/Weixin4jServerStartup.java @@ -3,6 +3,7 @@ package com.foxinmy.weixin4j.example.server; import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.handler.DebugMessageHandler; import com.foxinmy.weixin4j.startup.WeixinServerBootstrap; +import com.foxinmy.weixin4j.util.AesToken; /** * 微信消息服务:单独作为一个服务jar包启动,推荐这种方式去处理微信消息,可在本项目的根目录运行 `mvn package`得到一个可执行的zip包。 @@ -38,7 +39,7 @@ public final class Weixin4jServerStartup { * @throws WeixinException */ public static void main(String[] args) throws WeixinException { - new WeixinServerBootstrap(aesToken) // 指定开发者token信息。 + new WeixinServerBootstrap(new AesToken("wxa652fc930afe9b22", "weixin4j", "iFv2hlZm56rkwv5oC45UoIfvPJVHp2ngocBMbt5FP9C")) // 指定开发者token信息。 .handlerPackagesToScan(handlerPackage) // 扫描处理消息的包。 .addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息,开发环境打开。 .openAlwaysResponse() // 当没有匹配到消息处理时输出空白回复(公众号不会出现「该公众号无法提供服务的提示」),正式环境打开。 diff --git a/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/handler/TextMessageHandler.java b/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/handler/TextMessageHandler.java index d54f9a48..b3f1f16a 100644 --- a/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/handler/TextMessageHandler.java +++ b/weixin4j-example/src/main/java/com/foxinmy/weixin4j/example/server/handler/TextMessageHandler.java @@ -19,7 +19,6 @@ import com.foxinmy.weixin4j.response.WeixinResponse; */ @Component public class TextMessageHandler extends MessageHandlerAdapter { - @Override public WeixinResponse doHandle0(WeixinRequest request, TextMessage message) throws WeixinException {