Search code examples
javamicronaut

Multiple get request parameters @PathVariable and @RequestAttribute with Micronaut?


I have a get function in my controller with differents parameters:

myinterface.java:

public interface MyInterface {

   @Get(value = "/bob/{name}/params?surname={surname}")
   String getMyParam (
   @Parameter(name="name", required=true)
   @PathVariable("name") String name, 
   @NotNull 
   @Parameter(name="surname", required=true) 
   @Valid 
   @RequestAttribute(value="surname") String surname) {

   }
}

mycontroller.java:

public class MyController implements MyInterface {

   @Override
   public String getMyParam(String name, String surname) { return name + surname; }
}

But when i'm calling "http://localhost:8080/bob/marley/params?surname=lion" it's send a ERROR MESSAGE: Page Not Found.

When i'm work with optional parameters /books{?max,offset} it's work. Did i miss something?

Are PathVariable and RequestAttribute not mixable while doing query requests?

EDIT 1

When i remove the ?surname=={surname} from the @Get value it occurs an "HttpClientResponseException: Require argument [String surname] not specified".


Solution

  • surname in your case is not request attribute but query value (parameter). So use @QueryValue annotation instead of @RequestAttribute and do not specify it in URL pattern.

    So the controller implementation can look like this:

    @Controller
    public class MyController {
        @Get(value = "/bob/{name}/params")
        String getMyParam(String name, @QueryValue String surname) {
            return name + surname;
        }
    }
    

    Another thing is that @NotNull annotation for surname parameter is redundant. That parameter is required by default. If you want it as optional then it must be of Optional type like this:

    @Get(value = "/bob/{name}/params")
    String getMyParam(String name, @QueryValue Optional<String> surname) {
        return name + surname.orElse("");
    }