Skip to content

Commit 6132827

Browse files
committed
add PlaintextHandler
1 parent 584fa27 commit 6132827

5 files changed

Lines changed: 39 additions & 25 deletions

File tree

frameworks/Java/tio-boot/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<java.version>1.8</java.version>
1212
<maven.compiler.source>${java.version}</maven.compiler.source>
1313
<maven.compiler.target>${java.version}</maven.compiler.target>
14-
<tio-boot.version>2.0.1</tio-boot.version>
14+
<tio-boot.version>2.0.5</tio-boot.version>
1515

1616

1717
<main.class>com.litongjava.tio.http.server.MainApp</main.class>
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>com.litongjava</groupId>
2828
<artifactId>java-db</artifactId>
29-
<version>1.5.3</version>
29+
<version>1.5.5</version>
3030
</dependency>
3131

3232
<dependency>
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>com.alibaba.fastjson2</groupId>
4747
<artifactId>fastjson2</artifactId>
48-
<version>2.0.52</version>
48+
<version>2.0.60</version>
4949
</dependency>
5050

5151
<dependency>

frameworks/Java/tio-boot/src/main/java/com/litongjava/tio/http/server/MainApp.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.litongjava.tio.http.server;
22

33
import com.litongjava.tio.boot.TioApplication;
4+
import com.litongjava.tio.http.server.config.MainAppConfig;
45

56
public class MainApp {
67

frameworks/Java/tio-boot/src/main/java/com/litongjava/tio/http/server/MainAppConfig.java renamed to frameworks/Java/tio-boot/src/main/java/com/litongjava/tio/http/server/config/MainAppConfig.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
package com.litongjava.tio.http.server;
1+
package com.litongjava.tio.http.server.config;
22

33
import com.litongjava.context.BootConfiguration;
44
import com.litongjava.tio.boot.server.TioBootServer;
5-
import com.litongjava.tio.http.server.config.EhCachePluginConfig;
6-
import com.litongjava.tio.http.server.config.EnjoyEngineConfig;
7-
import com.litongjava.tio.http.server.config.MysqlDbConfig;
85
import com.litongjava.tio.http.server.handler.CacheHandler;
96
import com.litongjava.tio.http.server.handler.DbHandler;
10-
import com.litongjava.tio.http.server.handler.IndexHandler;
7+
import com.litongjava.tio.http.server.handler.JsonHandler;
8+
import com.litongjava.tio.http.server.handler.PlaintextHandler;
119
import com.litongjava.tio.http.server.router.HttpRequestRouter;
1210
import com.litongjava.tio.utils.environment.EnvUtils;
1311

@@ -33,13 +31,14 @@ public void config() throws Exception {
3331
}
3432

3533
// add route
36-
IndexHandler controller = new IndexHandler();
34+
JsonHandler jsonHanlder = new JsonHandler();
3735

36+
PlaintextHandler plaintextHandler = new PlaintextHandler();
3837
TioBootServer server = TioBootServer.me();
3938
HttpRequestRouter requestRouter = server.getRequestRouter();
4039
if (requestRouter != null) {
41-
requestRouter.add("/plaintext", controller::plaintext);
42-
requestRouter.add("/json", controller::json);
40+
requestRouter.add("/plaintext", plaintextHandler);
41+
requestRouter.add("/json", jsonHanlder);
4342

4443
DbHandler dbQueryController = new DbHandler();
4544
requestRouter.add("/db", dbQueryController::db);

frameworks/Java/tio-boot/src/main/java/com/litongjava/tio/http/server/handler/IndexHandler.java renamed to frameworks/Java/tio-boot/src/main/java/com/litongjava/tio/http/server/handler/JsonHandler.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.litongjava.tio.http.server.handler;
22

3-
import java.nio.charset.StandardCharsets;
4-
53
import com.alibaba.fastjson2.JSON;
64
import com.litongjava.tio.boot.http.TioRequestContext;
75
import com.litongjava.tio.http.common.HttpRequest;
@@ -13,22 +11,13 @@
1311
* ab -k -n1000000 -c10 http://127.0.0.1:8080/json ab -k -n1000000 -c10
1412
* http://127.0.0.1:8080/plaintext
1513
*/
16-
public class IndexHandler {
14+
public class JsonHandler implements HttpRequestHandler {
1715

1816
private static final String HELLO_WORLD = "Hello, World!";
19-
20-
private static final byte[] HELLO_WORLD_BYTES = HELLO_WORLD.getBytes(StandardCharsets.UTF_8);
2117
private static final byte[] JSON_BYTES = JSON.toJSONBytes(new Message(HELLO_WORLD));
2218

23-
public HttpResponse plaintext(HttpRequest request) {
24-
HttpResponse response = TioRequestContext.getResponse();
25-
response.setBody(HELLO_WORLD_BYTES);
26-
String mimeTypeStr = MimeTypeUtils.getTextUTF8();
27-
response.setContentType(mimeTypeStr);
28-
return response;
29-
}
30-
31-
public HttpResponse json(HttpRequest request) {
19+
@Override
20+
public HttpResponse handle(HttpRequest httpRequest) throws Exception {
3221
HttpResponse response = TioRequestContext.getResponse();
3322
response.setBody(JSON_BYTES);
3423
String mimeTypeStr = MimeTypeUtils.getJsonUTF8();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.litongjava.tio.http.server.handler;
2+
3+
import java.nio.charset.StandardCharsets;
4+
5+
import com.litongjava.tio.boot.http.TioRequestContext;
6+
import com.litongjava.tio.http.common.HttpRequest;
7+
import com.litongjava.tio.http.common.HttpResponse;
8+
import com.litongjava.tio.http.common.utils.MimeTypeUtils;
9+
10+
public class PlaintextHandler implements HttpRequestHandler {
11+
12+
private static final String HELLO_WORLD = "Hello, World!";
13+
14+
private static final byte[] HELLO_WORLD_BYTES = HELLO_WORLD.getBytes(StandardCharsets.UTF_8);
15+
16+
@Override
17+
public HttpResponse handle(HttpRequest httpRequest) throws Exception {
18+
HttpResponse response = TioRequestContext.getResponse();
19+
response.setBody(HELLO_WORLD_BYTES);
20+
String mimeTypeStr = MimeTypeUtils.getTextUTF8();
21+
response.setContentType(mimeTypeStr);
22+
return response;
23+
}
24+
25+
}

0 commit comments

Comments
 (0)