allora, seguendo il tuo consiglio ho fatto così:
codice:
import com.zara.api.zara_api.utils.JsonUtils;
import com.zara.api.zara_api.utils.JwtHelper;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
public class UtentiController {
private final JwtHelper jwtHelper;
public UtentiController(JwtHelper jwtHelper) {
this.jwtHelper = jwtHelper;
}
public void index(RoutingContext routingContext) {
HttpServerResponse response = routingContext.response();
response.putHeader("content-type", "text/plain").end("Utenti");
}
public void login(RoutingContext routingContext) {
HttpServerResponse response = routingContext.response();
if (!routingContext.body().isEmpty()) {
JsonObject jsonObject = routingContext.body().asJsonObject();
String username = jsonObject.getString("username");
String password = jsonObject.getString("password");
// ESEGUO QUERY
if (username.equals("matteo") && password.equals("132456")) {
response
.putHeader("content-type", "application/json")
.end(JsonUtils.loginJson("ok", "Login effettuato!", jwtHelper.relaeseToken()));
} else {
response
.setStatusCode(400)
.putHeader("content-type", "application/json")
.end(JsonUtils.genericJson("ko", "Credenziali errate!"));
}
} else {
response
.setStatusCode(400)
.putHeader("content-type", "application/json")
.end(JsonUtils.genericJson("ko", "Dati non presenti!"));
}
}
}
e poi nel routing:
codice:
JwtHelper jwtHelper = new JwtHelper();
UtentiController utentiController = new UtentiController(jwtHelper);
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
router.get("/").handler(ctx -> {
Utils utils = new Utils();
HttpServerResponse response = ctx.response();
response.putHeader("content-type", "text/plain");
response.end(utils.getVersion());
});
router.get("/utenti").handler(utentiController::index);
router.post("/utenti/login").handler(utentiController::login);
sembra funzionare tutto.
-------------
poi volevo capire se c'è la possibilità di raggruppare le route.
ho un pò cercato ma non ho trovato (probabilmente ho cercato male io).
per intenderci, e fare un esempio concreto, in laravel (PHP) si fa una cosa così:
codice:
Route::group(["prefix" => "articoli", "middleware" => ["auth:sanctum"]], function () {
Route::get('/', [ArticoliEcommerceController::class, 'index']);
Route::get('/{id}', [ArticoliEcommerceController::class, 'getById']);
Route::get('/brand/{id}', [ArticoliEcommerceController::class, 'getByBrand']);
Route::post('/add', [ArticoliEcommerceController::class, 'add']);
Route::post('/up_sconti', [ArticoliEcommerceController::class, 'upSconti']);
});
cosi ho "macro rotta" ha le sue rotte.
non so, ma non ho trovato ancora nulla al riguardo.