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()
}