简体   繁体   中英

Spring @GetMapping is being ignored

I have the following controller:

@RestController
@RequestMapping("/api/{brand}")
public class CarController {

  private final CarService carService;

  @Autowird
  public CarController(CarService carService) {
    this.carService = carService;
  }

  @GetMapping
  public Resources<Car> getCars(@PathVariable("brand") String brand) {
    return new Resources<>(carService.getCars(brand));
  }

  @GetMapping(value = "/{model}")
  public Car getModel(@PathVariable("brand") String brand, @PathVariable("model") String model) {
    return carService.getCar(brand, model);
  }
}

I would expect an http GET call to http://localhost:8080/api/bmw to return me the result of the getCars method. Instead, the call is delegated to the getModel method. This returns an error, because there is no {model} path variable.

How come my http calls are delegated to the incorrect @GetMapping ?

Here you can see the version of spring-boot-starter-web that I pull in via hateoas :

[INFO] +- org.springframework.boot:spring-boot-starter-hateoas:jar:2.1.9.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-web:jar:2.1.9.RELEASE:compile
[INFO] | | - org.springframework.boot:spring-boot-starter-tomcat:jar:2.1.9.RELEASE:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.26:compile
[INFO] | | - org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.26:compile
[INFO] | +- org.springframework.hateoas:spring-hateoas:jar:0.25.2.RELEASE:compile
[INFO] | - org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile

I've enabled the mappings endpoint of Spring Actuator and I can even see that the endpoint that is being ignored is available:

{
  "handler": "public org.springframework.hateoas.Resources<com.example.Car> com.example.CarController.getCars(java.lang.String)",
  "predicate": "{GET /api/{brand}, produces [application/hal+json]}",
  "details": {
    "handlerMethod": {
      "className": "com.example.CarController",
      "name": "getCars",
      "descriptor": "(Ljava/lang/String;)Lorg/springframework/hateoas/Resources;"
    },
    "requestMappingConditions": {
      "consumes": [],
      "headers": [],
      "methods": [
        "GET"
      ],
      "params": [],
      "patterns": [
        "/api/{brand}"
      ],
      "produces": [
        {
          "mediaType": "application/hal+json",
          "negated": false
        }
      ]
    }
  }
}

EDIT I've added an interceptor that enables me to see what the target handlerMethod will be.

The handlerMethod is the correct one:

public org.springframework.hateoas.Resources com.example.CarController.getCars(java.lang.String)

Yet I still get the following error:

Internal server error: Missing URI template variable 'model' for method parameter of type String

I can't wrap my head around the fact that the handlerMethod does not expect the model parameter, but spring still throws an error because of it.

In your case, @RequestMapping("/api/{brand}") expects an input brand which is not found as you have used the annotation at class level. You can correct it the following way:

@RestController
@RequestMapping("/api")
public class CarController {

  private final CarService carService;

  @Autowird
  public CarController(CarService carService) {
    this.carService = carService;
  }

  @GetMapping(value = "/{brand}")
  public Resources<Car> getCars(@PathVariable("brand") String brand) {
    return new Resources<>(carService.getCars(brand));
  }

  @GetMapping(value = "/{brand}/{model}")
  public Car getModel(@PathVariable("brand") String brand, @PathVariable("model") String model) {
    return carService.getCar(brand, model);
  }
}

So getCars() method will expect an input brand and getModel() will expect two inputs brand and model. Hope it helps!

Check your method mapping again:

As you said, you want to call gatCars method based on brand, you have to provide value in get mappings so function should be:

 @GetMapping(value = "/{model}")
  public Resources<Car> getCars(@PathVariable("brand") String brand) {
    return new Resources<>(carService.getCars(brand));
  }

Request is going to getModel cause it matches the signature. Correct the getModel signature as below.

http://localhost:8080/api/bmw/x5

  @GetMapping(value = "/{model}/{brand}")
  public Car getModel(@PathVariable("brand") String brand, @PathVariable("model") String model) {
    return carService.getCar(brand, model);
  }

I think that a path variable can not be put into annotation @RequestMapping for the entire controller class. I suggest changing your @RequestMapping("/api/{brand}") to @RequestMapping("/api") and then change

  @GetMapping
  public Resources<Car> getCars(@PathVariable("brand") String brand) {
    return new Resources<>(carService.getCars(brand));
  }

  @GetMapping(value = "/{model}")
  public Car getModel(@PathVariable("brand") String brand, @PathVariable("model") String model) {
    return carService.getCar(brand, model);
  }

to

  @GetMapping(value = "/{brand}")
  public Resources<Car> getCars(@PathVariable("brand") String brand) {
    return new Resources<>(carService.getCars(brand));
  }

  @GetMapping(value = "/{brand}/{model}")
  public Car getModel(@PathVariable("brand") String brand, @PathVariable("model") String model) {
    return carService.getCar(brand, model);
  }

It turns out that a @RestControllerAdvice was the culprit:

@RestControllerAdvice(assignableTypes = {CarController.class})
public class InterceptModelPathParameterControllerAdvice {

  @Autowired
  CarService carService;

  @ModelAttribute
  public void validateModel(@PathVariable("model") String model) {
    if (!carService.isSupportedModel(model)) throw new RuntimeException("This model is not supprted by this application.");
  }
}

Because the getCars method did not have a @PathVariable("model") , the exception was being thrown.

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