
cc.shacocloud.mirage.restful.example.ExampleServer Maven / Gradle / Ivy
package cc.shacocloud.mirage.restful.example;
import cc.shacocloud.mirage.restful.*;
import cc.shacocloud.mirage.restful.bind.annotation.*;
import cc.shacocloud.mirage.restful.bind.validation.BindingResult;
import cc.shacocloud.mirage.restful.bind.validation.Validated;
import cc.shacocloud.mirage.restful.bind.validation.errors.BindingResultError;
import cc.shacocloud.mirage.restful.exception.BindingException;
import cc.shacocloud.mirage.restful.exception.MethodArgumentNotValidException;
import cc.shacocloud.mirage.restful.http.MultipartFile;
import cc.shacocloud.mirage.restful.http.MultipartFileUpload;
import cc.shacocloud.mirage.utils.reflection.DefaultParameterNameDiscoverer;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerOptions;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.function.Function;
public class ExampleServer {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
startServer(vertx, new ExampleController());
}
/**
* 启动样例服务
*/
public static void startServer(Vertx vertx,
Object... handlers) {
startServer(vertx, 8080, handlers);
}
/**
* 启动样例服务
*/
public static void startServer(Vertx vertx,
int port,
Object... handlers) {
startServer(vertx, port, vertx1 -> new MirageRequestMappingHandler(vertx1, new DefaultParameterNameDiscoverer()), handlers);
}
/**
* 启动样例服务
*/
public static void startServer(Vertx vertx,
int port,
Function mappingHandlerFunc,
Object @NotNull ... handlers) {
// 构建 RequestMapping
MirageRequestMappingHandler requestMappingHandler = buildRouterMappingHandler(vertx, mappingHandlerFunc);
for (Object handler : handlers) {
requestMappingHandler.detectHandlerMethods(handler);
}
CountDownLatch downLatch = new CountDownLatch(1);
HttpServerOptions serverProperties = new HttpServerOptions();
serverProperties.setLogActivity(false);
serverProperties.setPort(port);
// 启动服务
VertxRouterDispatcherOptions routerDispatcherOptions = new VertxRouterDispatcherOptions();
routerDispatcherOptions.setServerOptions(serverProperties);
routerDispatcherOptions.setRouterMappingHandlers(
Arrays.asList(requestMappingHandler)
);
VertxRouterDispatcherHandler.createHttpServer(vertx, routerDispatcherOptions)
.onComplete(ar -> {
downLatch.countDown();
if (ar.failed()) throw new RuntimeException(ar.cause());
});
// 阻塞等待初始化完成
try {
downLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 构建路由映射处理器
*/
public static @NotNull MirageRequestMappingHandler buildRouterMappingHandler(Vertx vertx,
@NotNull Function mappingHandlerFunc) {
// 绑定请求处理映射器
MirageRequestMappingHandler mirageRequestMappingHandler = mappingHandlerFunc.apply(vertx);
// 异常处理器
ExceptionHandlerExceptionResolver exceptionResolver = mirageRequestMappingHandler.getExceptionHandlerExceptionResolver();
exceptionResolver.registerExceptionHandler(new DefaultExceptionHandler());
// 初始化
mirageRequestMappingHandler.init();
return mirageRequestMappingHandler;
}
// ------------------ 业务代码
/**
* 默认异常处理器
*/
@ResponseBody
public static class DefaultExceptionHandler {
@ExceptionHandler(Exception.class)
public Future
© 2015 - 2025 Weber Informatics LLC | Privacy Policy