From 63d81798e54dcdb569e8fdf8feb6cca9ef7b4bc2 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Wed, 31 May 2017 22:23:24 +0800 Subject: [PATCH] --- course01_hello.html | 122 +++++++++++++++++++++++++++----------------- images/hello_03.png | Bin 85482 -> 63847 bytes images/hello_04.png | Bin 104897 -> 95431 bytes images/hello_05.png | Bin 70258 -> 80363 bytes images/hello_06.png | Bin 49697 -> 38367 bytes images/hello_09.png | Bin 82290 -> 39261 bytes 6 files changed, 76 insertions(+), 46 deletions(-) diff --git a/course01_hello.html b/course01_hello.html index 5753ed3eb..f1c23c3f3 100644 --- a/course01_hello.html +++ b/course01_hello.html @@ -33,71 +33,101 @@
源码可以从 https://github.com/redkale 和 http://git.oschina.net/redkale/redkale 下载 。
jar包可以从 http://search.maven.org 和 https://repo1.maven.org/maven2/org/redkale/redkale/ 下载最新版本的包。
- 当前最新版为 1.8, 下载 redkale-1.8.tar.gz 放在本地。
+ 当前最新版为 1.8, 下载 redkale-1.8.0.tar.gz 放在本地。
创建工程
本人使用NetBeans很多年了,所以本教程以NetBeans来创建工程, 使用IntelliJ IDEA 或 Eclipse的同学请自行参考。
++ 首先创建一个"Java应用程序"项目, 注意: 不管是否Web项目,都不要创建Web应用程序。 +


+ 创建完空项目后,将 redkale-1.8.0.tar.gz 解压覆盖到项目的目录下。 +










public final <E> Encodeable<W, E> findEncoder(final Type type) {
- Encodeable<W, E> rs = (Encodeable<W, E>) encoders.get(type);
- if (rs != null) return rs;
- return this.parent == null ? null : this.parent.findEncoder(type);
-}- 当搜索不到Encoder、Decoder时,自身的ConvertFactory会自动创建一个ObjectEncoder、ObjectDecoder。 + 点击项目右键进入“属性”-> “库”中,点击 “添加JAR/文件夹”找到项目lib目录下的redkale-1.8.0.jar 并导入。
-public final <E> Encodeable<W, E> loadEncoder(final Type type) {
- Encodeable<W, E> encoder = findEncoder(type);
- if (encoder != null) return encoder;
- if (type instanceof GenericArrayType) return new ArrayEncoder(this, type);
- Class clazz;
- if (type instanceof ParameterizedType) {
- final ParameterizedType pts = (ParameterizedType) type;
- clazz = (Class) (pts).getRawType();
- } else if (type instanceof TypeVariable) {
- TypeVariable tv = (TypeVariable) type;
- Type t = Object.class;
- if (tv.getBounds().length == 1) {
- t = tv.getBounds()[0];
- }
- if (!(t instanceof Class)) t = Object.class;
- clazz = (Class) t;
- } else if (type instanceof Class) {
- clazz = (Class) type;
- } else {
- throw new ConvertException("not support the type (" + type + ")");
+ 

+
+ 点击“源”,在下面测试包中导入项目目录下的conf目录,这样方便编辑conf下的配置文件(在上面src中导入会打包进jar中)。
+
+ 
+
+ 点击“运行”,在主类中输入 "org.redkale.boot.Application",然后点击“确定”。
+
+ 
+
+ 编写HelloService类。
+
+ package com.redkale.demo;
+
+import org.redkale.net.http.*;
+import org.redkale.service.Service;
+
+@RestService(automapping = true)
+public class HelloService implements Service {
+
+ public String sayHello() {
+ return "Hello World!";
}
- encoder = findEncoder(clazz);
- if (encoder != null) return encoder;
- return createEncoder(type, clazz);
-}
+
+ public String hi(String name) {
+ return "Hi, " + name + "!";
+ }
+}
+ - 大部分情况下Convert的处理对象会根据JavaBean类自定生成,而有些场景需要覆盖处理类,这样需要子ConvertFactory,如 Convert基本用法 例子中使用JsonFactory.root().createChild()重定义。且与JsonFactory.root()中的定义可以并存,也不会产出冲突。 + 此类提供两个方法:say 和 hi。编写完后按"F6" 直接运行。
+
- Redkale可以启动多个协议Server服务(配置文件中含多个server节点),为避免冲突,每个非SNCP的Server的ResourceFactory也是独立的。 + 在浏览器输入: http://127.0.0.1:6060/pipes/hello/say 可以看到结果:
-public NodeServer(Application application, Server server) {
- this.application = application;
- this.resourceFactory = application.getResourceFactory().createChild();
- this.server = server;
- this.logger = Logger.getLogger(this.getClass().getSimpleName());
-}
- 双亲委托模型既可让同级子Factory保持独立,也可重用父Factory内的配置,因此在Redkale这种支持多Server、多种配置的场景下很是适合。 + 在浏览器输入: http://127.0.0.1:6060/pipes/hello/hi?name=Redkale 可以看到结果:
+
+ 访问地址的端口6060和前缀pipes是通过conf/application.xml文件进行配置: +
+
+ 至此,一个简单的Hello服务就开发和调试完成了。可以看出,代码简单很多,不需要太多配置、maven和其他依赖包。
+ 可能有人会疑惑: HelloServie为什么能分配到hello前缀? sayHello为什么会映射到/pipes/hello/say 请求?Redkale为了减少Annotation配置采取了一些默认值的策略, 如果标记@RestService没有指定name时,Redkale会采用去掉Service及后面部分的类名的小写作为模块名,如HelloService和HelloServiceImpl都会采用hello作为模块名。方法名的默认值策略则是去掉模块名字样及后面部分的方法名小写,如sayHello和sayHelloMe 都会用say作为默认值。参数名如果没有指定@RestParam.name会自动采用代码的变量名。 完全标记Rest注解的HelloService源码如下:
+
package com.redkale.demo;
+
+import org.redkale.net.http.*;
+import org.redkale.service.Service;
+
+@RestService(name = "Hello")
+public class HelloService implements Service {
+
+ @RestMapping(name = "say")
+ public String sayHello() {
+ return "Hello World!";
+ }
+
+ @RestMapping(name = "hi")
+ public String hi(@RestParam(name = "name") String name) {
+ return "Hi, " + name + "!";
+ }
+
+}
+ - 转载请注明出处:https://redkale.org/article_parents.html + 这段代码与上面那段是等价的。部署也很简单, 将项目目录中dist目录下的redkale-demo.jar复制到lib下,运行bin/start.bat 即可启动HTTP服务。 +
++ 转载请注明出处:https://redkale.org/course01_hello.html