Skip to content

Instantly share code, notes, and snippets.

View digimon1740's full-sized avatar
🎯
Focusing

sanghoon lee digimon1740

🎯
Focusing
View GitHub Profile
fun main () {
val differ = differString("abc", "abd") { index, a, b ->
a != b
}
println("differ : $differ")
}
fun differString(a: String, b: String, predicate: (index: Int, a: Char, b: Char) -> Boolean): Boolean {
val org = a.toCharArray()
val diff = b.toCharArray()
@digimon1740
digimon1740 / UserController.java
Created April 24, 2020 17:33
using ResponseEntity on Spring WebFlux
@RestController
public class UserController {
private UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@OpenApi(
requestBody = OpenApiRequestBody(User::class),
responses = [
OpenApiResponse("400", Unit::class),
OpenApiResponse("201", Unit::class)
]
)
fun addUserHandler(ctx: Context) {
val user = ctx.body()
UserRepository.createUser(user)
val addUserDocs = document()
.body()
.result("400")
.result("204")
fun addUserHandler(ctx: Context) {
val user = ctx.body()
UserRepository.addUser(user)
ctx.status(204)
}
// 액세스 매니저 설정
app.accessManager((handler, ctx, permittedRoles) -> {
MyRole userRole = getUserRole(ctx);
if (permittedRoles.contains(userRole)) {
handler.handle(ctx);
} else {
ctx.status(401).result("Unauthorized");
}
});
Role getUserRole(Context ctx) {
//before handlers
app.before(ctx -> {
// 모든 요청의 앞단에서 동작합니다.
});
app.before("/path/*", ctx -> {
// /path/* 로 시작하는 모든 요청의 앞단에서 동작합니다.
});
//endpoint handlers
app.get("/", ctx -> {
// 구현 로직
// 검증이 없는 경우, String 또는 null을 리턴합니다.
var myQpStr = ctx.queryParam("my-qp");
// Integer 또는 throws를 발생시킵니다.
var myQpInt = ctx.pathParam("my-qp", Integer.class).get();
// 이 코드는 if (Integer > 4)와 동일합니다.
var myQpInt = ctx.formParam("my-qp", Integer.class).check(i -> i > 4).get();
// 두개의 의존적인 파라미터의 검증 방법
var fromDate = ctx.queryParam("from", Instant.class).get();
var toDate = ctx.queryParam("to", Instant.class)
.check(it -> it.isAfter(fromDate), "'to' has to be after 'from'")
var app = Javalin.create(config -> {
config.defaultContentType = "application/json";
config.autogenerateEtags = true;
config.addStaticFiles("/public");
config.asyncRequestTimeout = 10_000L;
config.dynamicGzip = true;
config.enforceSsl = true;
}).routes(() -> {
path("users", () -> {
get(UserController::getAll);
public static void main(String[] args) {
var app = Javalin.create().start(7000);
app.get("/", ctx -> ctx.result("Hello World"));
}
if (it != null) {
ok().build()
}