Home > Back-end >  Quarkus parameter is always null
Quarkus parameter is always null

Time:09-16

Hi so i were trying out the redis cache shown here quarkus redis My increment class

public class Increment {
    public String key; 
    public long value; 

    public Increment(String key, long value) {
        this.key = key;
        this.value = value;
    }

    public Increment() {
    }
}

resource file

@Path("/increments")
public class IncrementResource {
    @Inject
    IncrementService service;

    @POST
    public Increment create(Increment increment) {
        service.set(increment.key, increment.value);
        return increment;
    }

    @GET
    @Path("/{key}")
    public Increment get(String key) {
        System.out.println(key); // always null
        return new Increment(key, service.get(key));
    }

}

service file

@ApplicationScoped
public class IncrementService {

    private StringCommands<String, Long> countCommands; 
    
    public IncrementService(RedisDataSource ds) { 
        this.countCommands = ds.string(Long.class); 
    }

   long get(String key) {
        System.out.println("getting "   key);
        Long value = countCommands.get(key); 
        System.out.println("getting "   value);
        if (value == null) {
            return 0L;
        }
        return value;
    }

    void set(String key, Long value) {
        System.out.println("setting "   key   value);
        countCommands.set(key, value, new SetArgs().ex(Duration.ofSeconds(10)));
    }

    void increment(String key, Long incrementBy) {
        countCommands.incrby(key, incrementBy); 
    }

    public void evict(String key) {
        countCommands.getdel(key);
    }
}

Whenever I run this it works fine. However, the key variable in the resource file GET method is always null. Not sure why, have worked on previous projects

CodePudding user response:

Your GET method is missing the @PathParam annotation before String key

@GET
@Path("/{key}")
public Increment get(@PathParam String key) {
    System.out.println(key); // always null
    return new Increment(key, service.get(key));
}
  • Related