Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use datasource in mvc class #2672

Closed
paliwalsam opened this issue Nov 11, 2022 · 1 comment
Closed

how to use datasource in mvc class #2672

paliwalsam opened this issue Nov 11, 2022 · 1 comment
Labels
Milestone

Comments

@paliwalsam
Copy link

How to use hikari datasource in MVC class

File: App.java

package app;

import io.jooby.Jooby;
import io.jooby.hikari.HikariModule;
import javax.sql.DataSource;

public class App extends Jooby {

{
	install(new HikariModule());
	mvc(new Usercontroller());  
	DataSource DS = require(DataSource.class);
    }

public static void main(final String[] args) {
    runApp(args, App::new);
}

}

File: Usercontroller.java

package app;

import io.jooby.Jooby;
import io.jooby.annotations.*;
import io.jooby.MediaType;
import io.jooby.Context;

@path("/api-v1")
public class Usercontroller {

public String sayHi(Context ctx) {
    // want to use Hikari DataSource here
    ctx.setResponseCode(200);
    ctx.setResponseType(MediaType.json);
    String q = ctx.query("q").value();
    return "{\"message\": \"Hello Raw Response" + q + "\"}";
}

@Path("/pvc")
@GET
public String sayHi2(Context ctx) {
    //ctx.setResponseCode(401);                  
    ctx.setResponseType(MediaType.json);
    return "{\"message\": \"Hello PVC Response\"}";
}

}

No documentation found, plz help

@jknack
Copy link
Member

jknack commented Nov 26, 2022

Hi:

public String sayHi(Context ctx) {
    // want to use Hikari DataSource here
    DataSource ds = ctx.require(DataSource.class);

    ctx.setResponseCode(200);
    ctx.setResponseType(MediaType.json);
    String q = ctx.query("q").value();
    return "{\"message\": \"Hello Raw Response" + q + "\"}";
}

@jknack jknack added this to the 2.16.2 milestone Nov 26, 2022
@jknack jknack closed this as completed Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants