控制器参数给出一个奇怪的错误
Controller Parameter giving a strange error
我写了一个项目,使用 spring 引导我的控制器在我添加之前工作正常
'params' 获取映射注释
下面是我的 PlaneTypeVersionedApi 接口;
@Validated
@RequestMapping("/version")
public interface PlaneTypeVersionedApi {
@GetMapping(value = "/plane", params = "v1")
ResponseEntity<String> getOnePlaneByProduce1(@RequestParam("plane-type")String planeType);
}
我的 PlaneTypeVersionedApiImpl Class 下面;
@RestController
public class PlaneTypeVersionedApiImpl implements PlaneTypeVersionedApi {
private final PlaneCallerService planeCallerService;
public PlaneTypeVersionedApiImpl (PlaneCallerService planeCallerService) {
this.planeCallerService = planeCallerService;
}
@Override
public ResponseEntity<String> getOnePlaneByProduce1(String planeType) {
return ResponseEntity.ok(planeCallerService.getPlaneType(planeType));
}
}
当我为我的控制器尝试 Postman 时;
localhost:9080/version/plane?plane-type=light-weight?v1
我有 400 个错误的请求并说
已解决 [org.springframework.web.bind.UnsatisfiedServletRequestParameterException: 实际请求参数不满足参数条件“v1”:plane-type={light-weight?v1}]
url 上的查询参数语法无效。
这是正确的语法:localhost:9080/version/plane?plane-type=light-weight&v1.
https://launchschool.com/books/http/read/what_is_a_url#querystringsparameters
你的 url 不好,不确定你对 params
的用途
示例 1 有效 url = localhost:9080/version/plane?plane-type=light-weight&v1=
使用@RequestParam("plane-type") String plane-type
@RequestParam("v1") String v1 不需要使用属性 params
就足以获取值
示例 2 如果您想使用 params
@GetMapping(value = "/plane", params = {"v1"}) 这意味着你想要 v1 value
ResponseEntity<String> getOnePlaneByProduce1(@RequestParam("v1")String v1);
示例 3 两者都想要
@GetMapping(value = "/plane", params = {"v1","plane-type"}) 这意味着你想要 v1值和平面类型
ResponseEntity<String> getOnePlaneByProduce1(@RequestParam("v1")String v1, @RequestParam("plane-type") String plane-type);
params 选项只是意味着端点需要有那些参数,如果你指定的参数不在那里它会抱怨
我写了一个项目,使用 spring 引导我的控制器在我添加之前工作正常 'params' 获取映射注释
下面是我的 PlaneTypeVersionedApi 接口;
@Validated
@RequestMapping("/version")
public interface PlaneTypeVersionedApi {
@GetMapping(value = "/plane", params = "v1")
ResponseEntity<String> getOnePlaneByProduce1(@RequestParam("plane-type")String planeType);
}
我的 PlaneTypeVersionedApiImpl Class 下面;
@RestController
public class PlaneTypeVersionedApiImpl implements PlaneTypeVersionedApi {
private final PlaneCallerService planeCallerService;
public PlaneTypeVersionedApiImpl (PlaneCallerService planeCallerService) {
this.planeCallerService = planeCallerService;
}
@Override
public ResponseEntity<String> getOnePlaneByProduce1(String planeType) {
return ResponseEntity.ok(planeCallerService.getPlaneType(planeType));
}
}
当我为我的控制器尝试 Postman 时;
localhost:9080/version/plane?plane-type=light-weight?v1
我有 400 个错误的请求并说 已解决 [org.springframework.web.bind.UnsatisfiedServletRequestParameterException: 实际请求参数不满足参数条件“v1”:plane-type={light-weight?v1}]
url 上的查询参数语法无效。
这是正确的语法:localhost:9080/version/plane?plane-type=light-weight&v1.
https://launchschool.com/books/http/read/what_is_a_url#querystringsparameters
你的 url 不好,不确定你对 params
的用途示例 1 有效 url = localhost:9080/version/plane?plane-type=light-weight&v1=
使用@RequestParam("plane-type") String plane-type @RequestParam("v1") String v1 不需要使用属性 params
就足以获取值示例 2 如果您想使用 params
@GetMapping(value = "/plane", params = {"v1"}) 这意味着你想要 v1 value
ResponseEntity<String> getOnePlaneByProduce1(@RequestParam("v1")String v1);
示例 3 两者都想要
@GetMapping(value = "/plane", params = {"v1","plane-type"}) 这意味着你想要 v1值和平面类型
ResponseEntity<String> getOnePlaneByProduce1(@RequestParam("v1")String v1, @RequestParam("plane-type") String plane-type);
params 选项只是意味着端点需要有那些参数,如果你指定的参数不在那里它会抱怨