简体   繁体   中英

Quarkus REST API with Hibernate/Panache - Endpoints querying Postgres Stored Procedure

is there a particularly good way to do this? I have used Panache/Hibernate ORM to extend the PanacheEntity to create a mapping for the schema for a new table. Everything works as expected using the Repository method and I have the proper endpoints that reflect GETs, PUTs, etc. My current issue is that I was trying to have an entirely different endpoint that ONLY does a GET on a Postgresql Function/Stored Procedure and return that data when you hit that endpoint. This is the endpoint -

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;



@Path("/holidays")
@Produces("application/json")
@Consumes("application/json")
public class HolidayResource {

    @Inject
    EntityManager entityManager;

    @GET
    public Holiday[] get() {
        return entityManager.createNamedQuery("Holidays.findAll", Holiday.class)
                .getResultList().toArray(new Holiday[0]);
    }
    
}

And this is the object/class -

import java.sql.Date;
import java.sql.Timestamp;

import javax.persistence.Entity;
import javax.persistence.Id;

import org.hibernate.annotations.NamedNativeQuery;


@Entity
@NamedNativeQuery(name = "Holidays.findAll", query = "SELECT * FROM holidays.usa('NY', 2020, 2020)")
public class Holiday {


    public static enum Authority {
        federal,
        national,
        bank,
        provincial,
        state,
        informal,
        observance,
        shortened_work_day,
        optional,
        de_facto,
        religious,
        extra_work_day,
        municipal
    }

    @Id public long id;
    public Date datestamp;
    public String description;
    public Authority authority;
    public Boolean day_off;
    public Boolean observation_shifted;
    public Timestamp start_time;
    public Timestamp end_time;

}

Note that I'm not trying to create a Table or anything only just to display. This is the stack trace, but I've tried a few things that seem to kinda have me going in circles (such as adding getters/setters and other things) -

java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.IllegalArgumentException: SRCFG00013: No Converter registered for interface java.nio.file.Path
        at io.quarkus.vertx.http.runtime.VertxHttpRecorder.startServerAfterFailedStart(VertxHttpRecorder.java:223)
        at io.quarkus.vertx.http.runtime.devmode.VertxHttpHotReplacementSetup.handleFailedInitialStart(VertxHttpHotReplacementSetup.java:37)
        at io.quarkus.deployment.dev.RuntimeUpdatesProcessor.startupFailed(RuntimeUpdatesProcessor.java:662)
        at io.quarkus.deployment.dev.IsolatedDevModeMain.firstStart(IsolatedDevModeMain.java:137)
        at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:378)
        at io.quarkus.deployment.dev.IsolatedDevModeMain.accept(IsolatedDevModeMain.java:56)
        at io.quarkus.bootstrap.app.CuratedApplication.runInCl(CuratedApplication.java:127)
        at io.quarkus.bootstrap.app.CuratedApplication.runInAugmentClassLoader(CuratedApplication.java:84)
        at io.quarkus.deployment.dev.DevModeMain.start(DevModeMain.java:144)
        at io.quarkus.deployment.dev.DevModeMain.main(DevModeMain.java:63)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.IllegalArgumentException: SRCFG00013: No Converter registered for interface java.nio.file.Path
        at io.quarkus.runtime.configuration.ConfigInstantiator.handleObject(ConfigInstantiator.java:106)
        at io.quarkus.runtime.configuration.ConfigInstantiator.handleObject(ConfigInstantiator.java:57)
        at io.quarkus.vertx.http.runtime.VertxHttpRecorder.startServerAfterFailedStart(VertxHttpRecorder.java:195)
        ... 9 more
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.IllegalArgumentException: SRCFG00013: No Converter registered for interface java.nio.file.Path
        at io.quarkus.runtime.configuration.ConfigInstantiator.handleObject(ConfigInstantiator.java:106)
        at io.quarkus.runtime.configuration.ConfigInstantiator.handleObject(ConfigInstantiator.java:79)
        ... 11 more
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: SRCFG00013: No Converter registered for interface java.nio.file.Path
        at io.quarkus.runtime.configuration.ConfigInstantiator.handleObject(ConfigInstantiator.java:106)
        at io.quarkus.runtime.configuration.ConfigInstantiator.handleObject(ConfigInstantiator.java:79)
        ... 12 more
Caused by: java.lang.IllegalArgumentException: SRCFG00013: No Converter registered for interface java.nio.file.Path
        at io.smallrye.config.SmallRyeConfig.lambda$getConverter$2(SmallRyeConfig.java:292)
        at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705)
        at io.smallrye.config.SmallRyeConfig.getConverter(SmallRyeConfig.java:289)
        at io.quarkus.runtime.configuration.ConfigInstantiator.getConverterFor(ConfigInstantiator.java:121)
        at io.quarkus.runtime.configuration.ConfigInstantiator.getConverterFor(ConfigInstantiator.java:117)
        at io.quarkus.runtime.configuration.ConfigInstantiator.handleObject(ConfigInstantiator.java:91)
        ... 13 more

The Postgres function/stored procedure was created off a Python script that I found somewhere and it would be invoked like so for example and is reflected in the @NamedNativeQuery annotation. -

SELECT * from holidays.usa('NY', 2020, 2020);

An example of the return from the Query -

datestamp       description                authority      day_off      observation_shifted   start_time   end_time        
[DATE]          [TEXT]                     [ENUM]         [BOOLEAN]    [BOOLEAN]             [TIME]       [TIME]
------------    -----------------------    -----------    ---------    -------------------   ----------   ----------
"2020-01-01"    "New Year's Day"           "federal"       true        false                 "00:00:00"   "24:00:00"
"2020-02-17"    "Family Day"               "provincial"    true        false                 "00:00:00"   "24:00:00

Is there a better way to do this? A few of the things I've tried seem to drive me into the wrong circle.

You can define a @NamedStoredProcedure :

@NamedStoredProcedureQuery(
    name = "Holidays.findAll", 
    procedureName = "holidays.usa", 
    resultClasses = Holiday.class, 
    parameters = {
            @StoredProcedureParameter(
                name = "city",
                type = String.class,
                mode = ParameterMode.IN
            ),
            @StoredProcedureParameter(
                name = "year",
                type = Integer.class,
                mode = ParameterMode.IN
            ),
            // Additional parameters
            ...
        }
)

@Entity
public class Holiday {
}
List<Holiday> holidays = entityManager
.createNamedStoredProcedureQuery( "Holidays.findAll" )
.setParameter( "city", "NY" )
.setParameter( "year", 2020 )
 ...
.getResultList();

Or you can call stored procedures via the entityManager :

StoredProcedureQuery query = entityManager.createStoredProcedureQuery( "holidays.usa", Holiday.class);
query.registerStoredProcedureParameter( "city", String.class, ParameterMode.IN);
query.registerStoredProcedureParameter( "year", Integer.class, ParameterMode.IN);
... // register other parameters

query.setParameter("city", "NY");
query.setParameter("year", 2020);
... // set other parameters
List<Holiday> holidays = query.getResultList();

For future readers that landed here googling SRCFG00013: No Converter registered for interface java.nio.file.Path like I did:

I ran into a similar issue and got the same SRCFG00013 error message, but as stupid as this sounds, the solution was to scroll up in the log files, as the real hibernate stack trace with a meaningful message is way up above this cryptic SRCFG00013 errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM