1- package com .litongjava .tio .http .server .controller ;
1+ package com .litongjava .tio .http .server .handler ;
22
33import com .alibaba .fastjson2 .JSON ;
44import com .litongjava .tio .boot .http .TioRequestContext ;
5- import com .litongjava .tio .http .common .HeaderName ;
6- import com .litongjava .tio .http .common .HeaderValue ;
75import com .litongjava .tio .http .common .HttpRequest ;
86import com .litongjava .tio .http .common .HttpResponse ;
7+ import com .litongjava .tio .http .common .utils .MimeTypeUtils ;
98import com .litongjava .tio .http .server .model .Message ;
10- import com .litongjava .tio .http .server .util .Resps ;
119
1210/**
1311 * ab -k -n1000000 -c10 http://127.0.0.1:8080/json
1412 * ab -k -n1000000 -c10 http://127.0.0.1:8080/plaintext
1513 */
1614public class IndexHandler {
15+ private static final String HELLO_WORLD_RN = "Hello, World!\r \n " ;
16+
1717 private static final String HELLO_WORLD = "Hello, World!" ;
1818
19- private static final byte [] HELLO_WORLD_BYTES = HELLO_WORLD .getBytes ();
20-
21- public HttpResponse index (HttpRequest request ) {
22- return Resps .txt (request , "tio-boot" );
23- }
19+ private static final byte [] HELLO_WORLD_BYTES = HELLO_WORLD_RN .getBytes ();
20+ private static byte [] JSON_BYTES = JSON .toJSONBytes (new Message (HELLO_WORLD ));
2421
2522 public HttpResponse plaintext (HttpRequest request ) {
2623 HttpResponse response = TioRequestContext .getResponse ();
2724 response .setBody (HELLO_WORLD_BYTES );
28- response .addHeader (HeaderName .Content_Type , HeaderValue .Content_Type .TEXT_PLAIN_TXT );
25+ String mimeTypeStr = MimeTypeUtils .getTextUTF8 ();
26+ response .setContentType (mimeTypeStr );
2927 return response ;
3028 }
3129
3230 public HttpResponse json (HttpRequest request ) {
3331 HttpResponse response = TioRequestContext .getResponse ();
34- response .setBody (JSON .toJSONBytes (new Message (HELLO_WORLD )));
35- response .addHeader (HeaderName .Content_Type , HeaderValue .Content_Type .TEXT_PLAIN_JSON );
32+ response .setBody (JSON_BYTES );
33+ String mimeTypeStr = MimeTypeUtils .getJsonUTF8 ();
34+ response .setContentType (mimeTypeStr );
3635 return response ;
3736 }
3837}
0 commit comments