This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
public class UserController { | |
private UserService userService; | |
public UserController(UserService userService) { | |
this.userService = userService; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val addUserDocs = document() | |
.body() | |
.result("400") | |
.result("204") | |
fun addUserHandler(ctx: Context) { | |
val user = ctx.body() | |
UserRepository.addUser(user) | |
ctx.status(204) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 액세스 매니저 설정 | |
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//before handlers | |
app.before(ctx -> { | |
// 모든 요청의 앞단에서 동작합니다. | |
}); | |
app.before("/path/*", ctx -> { | |
// /path/* 로 시작하는 모든 요청의 앞단에서 동작합니다. | |
}); | |
//endpoint handlers | |
app.get("/", ctx -> { | |
// 구현 로직 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 검증이 없는 경우, 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'") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
var app = Javalin.create().start(7000); | |
app.get("/", ctx -> ctx.result("Hello World")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (it != null) { | |
ok().build() | |
} |
NewerOlder