test(forest): 一点小测试
This commit is contained in:
@@ -0,0 +1,24 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.forest;
|
||||||
|
|
||||||
|
import com.dtflys.forest.Forest;
|
||||||
|
import com.dtflys.forest.annotation.Get;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @date 2024-01-24
|
||||||
|
*/
|
||||||
|
public class TestClient {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
TestService testService = Forest.client(TestService.class);
|
||||||
|
System.out.println(testService.success());
|
||||||
|
System.out.println(testService.error());
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface TestService {
|
||||||
|
@Get("http://localhost:8000/success")
|
||||||
|
String success();
|
||||||
|
|
||||||
|
@Get("http://localhost:8000/error")
|
||||||
|
String error();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package com.lanyuanxiaoyao.service.forest;
|
||||||
|
|
||||||
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
|
import com.sun.net.httpserver.HttpHandler;
|
||||||
|
import com.sun.net.httpserver.HttpServer;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lanyuanxiaoyao
|
||||||
|
* @date 2024-01-24
|
||||||
|
*/
|
||||||
|
public class TestServer {
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
|
||||||
|
server.createContext("/success", new SuccessHandler());
|
||||||
|
server.createContext("/error", new ErrorHandler());
|
||||||
|
server.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class SuccessHandler implements HttpHandler {
|
||||||
|
@Override
|
||||||
|
public void handle(HttpExchange exchange) throws IOException {
|
||||||
|
exchange.sendResponseHeaders(200, 0);
|
||||||
|
OutputStream body = exchange.getResponseBody();
|
||||||
|
body.write("Success".getBytes());
|
||||||
|
body.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ErrorHandler implements HttpHandler {
|
||||||
|
@Override
|
||||||
|
public void handle(HttpExchange exchange) throws IOException {
|
||||||
|
exchange.sendResponseHeaders(500, 0);
|
||||||
|
OutputStream body = exchange.getResponseBody();
|
||||||
|
body.write("Error".getBytes());
|
||||||
|
body.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user