- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 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/
我有两个方法@GetMapping和@GetMapping("/{id}") @RestController("/user"){ public class UserRestController { @
我想将编码添加到映射 Controller 的响应中。但是由于某种原因出现了编译错误。只需要一个值数组 如何在响应中添加编码? 最佳答案 @GetMapping("/cards", produces
有时可能会发生这样的情况:数据库中什么都没有,方法 .findAll() 没有任何内容可显示并返回空主体。我知道我有这个函数的返回类型“List”,并且我不能直接返回字符串,但是如果列表为空,我如何以
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 7 年前。 我知道周围有很多类似的帖子,但没
我正在尝试为 @GetMapping 创建多条路线。例如,localhost:8080/tasks 和 localhost:8080/tasks/?status=... 所以我创建了如下几个方法。 C
Image for clarification我是 Spring MVC 的新手,我正在 Maven 上做这个项目。我试图导入 @GetMapping 但它没有被导入。可能是什么问题?有人可以帮我吗?
我只是创建一个 map 项目并执行此代码。但是有 getMap() 错误。我尝试了一些来自互联网的示例代码,但 getMap() 函数中也存在同样的错误。代码是: private Google
private GoogleMap mMap; 我从 MainActivity 开始一个类似的 Activity : @SuppressWarnings("StatementWithEmptyBody
我正在尝试使用堆栈溢出答案之一的交互式信息窗口。 链接如下: interactive infowindow 但是我在代码中使用 getMap() 时遇到错误。虽然我尝试使用 getMapAsync 但
我试图在不依赖 application.properties 中的 server.contextPath 的情况下创建我的路由 这是一个例子: @PreAuthorize("hasRole('ROLE
这个问题在这里已经有了答案: Cannot resolve method getMap() (2 个答案) 关闭 4 年前。 我对这段代码有疑问,“无法解析方法 getMap()。我没有找到问题出在
我正在尝试让一个 map fragment 在我的应用程序中工作,但在尝试获取我的 GoogleMap 对象时仍然出现错误。 FragmentWithMap.java import android.M
将 Leaflet 添加到我的 AngularJS 应用程序后: 并进行设置: // Initialise the feature group to store editable layers va
在我的 Controller 中,@GetMapping 的以下使用有效: @GetMapping(value = "/new") public String newEssay(){ retu
我有以下 Controller : @RestController @RequestMapping("/api/{brand}") public class CarController { pri
我使用处理程序从支持 map fragment 中获取 GoogleMap。我真的迷路了,几天来一直在努力修复它。 map 加载正常,但我怀疑它返回的是空值。我知道可能有一些不好的做法,但这不是我的问
我在 Spring boot 中遇到了 @GetMapping 的问题。 关于我的 @GetMapping 函数,在从数据库中获取所有数据时,它没有在此模型上序列化我的 id: //User.java
在尝试创建 GoogleMap 对象(它返回 null)时,我在让 .getMap() 工作时遇到了很多麻烦,我环顾四周,发现人们也遇到了类似的问题,但无法从他们那里找到任何帮助。在当前的实现中,我如
在尝试了几乎所有方法之后,我似乎无法在不提取空对象引用的情况下获取 map 。我试图将一个 google mapfragment 膨胀成一个 fragment ,但是每次我这样做时我总是保留一个 ge
这个问题在这里已经有了答案: Cannot resolve method getMap() (2 个答案) 关闭 4 年前。 我有一个在手机上运行良好的 GPS 应用程序。但是,我看到有人在生产中出
我是一名优秀的程序员,十分优秀!