gpt4 book ai didi

java - Spring @GetMapping 被忽略

转载 作者:行者123 更新时间:2023-11-30 05:19:02 27 4
gpt4 key购买 nike

我有以下 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);
}
}

我希望对 http://localhost:8080/api/bmw 进行 http GET 调用,以返回 getCars 方法的结果。相反,该调用将委托(delegate)给 getModel 方法。这会返回错误,因为没有 {model} 路径变量。

为什么我的 http 调用会被委托(delegate)给不正确的 @GetMapping

在这里您可以看到我通过 hateoas 引入的 spring-boot-starter-web 版本:

[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

我已经启用了 Spring Actuator 的 mappings 端点,我什至可以看到被忽略的端点可用:

{
"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
}
]
}
}
}

编辑我添加了 interceptor这使我能够看到目标 handlerMethod 是什么。

handlerMethod 是正确的:

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

但我仍然收到以下错误:

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

我无法理解 handlerMethod 不需要 model 参数这一事实,但 spring 仍然会因此抛出错误。

最佳答案

在您的情况下, @RequestMapping("/api/{brand}") 需要一个输入品牌,但由于您在类级别使用了注释而未找到该品牌。您可以通过以下方式更正它:

@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);
}
}

因此 getCars() 方法将需要一个输入品牌,而 getModel() 将需要两个输入品牌和型号。希望对您有帮助!

关于java - Spring @GetMapping 被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59839468/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com