This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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