【问题标题】:GET method request is working for all other type of requestsGET 方法请求适用于所有其他类型的请求
【发布时间】:2019-09-25 09:49:30
【问题描述】:

将资源部署到服务器后,我们开始进行 API 测试。 任何 API 将如何以 https://.. 开头,对于这些请求 GET POST.. 每个方法都可以正常工作,但是如果我将其更改为 http 而不是 https 对于每个方法都可以正常工作,其中 GET 方法 API(@ 987654322@) 即使您从 POST、PUT 等发出请求,也会响应。

我也尝试了这些更改

@Override 
protected void configure(HttpSecurity http) throws Exception {
}
@GetMapping("/getalluserdetails")
    public UserDetailResponser getAllUserDetails(
            @Valid @RequestHeader("accessToken") 
            @NotEmpty(message = "accessToken is mandatory") String bearer,
            @RequestHeader("mappingId") 
            @NotEmpty(message = "mappingId is mandatory") String mappingId) {
    }

即使我从 POST 发送请求,此方法也接受请求,只有当我将其更改为 http://.....request

实际的东西 (@Getmapping("/getalluserdetails")) 不应该适用于其他方法

【问题讨论】:

  • 请正确格式化您的代码。不可读。
  • 你有任何类级别的“REST”注释吗?
  • 是的,我有..(@restcontroller)

标签: java spring rest spring-boot jpa


【解决方案1】:

您可以使用@RequestMapping 注解来定义应将哪种类型的请求映射到函数。

@RequestMapping(method = { RequestMethod.GET })
@GetMapping("/getalluserdetails")
public UserDetailResponser getAllUserDetails(@Valid @RequestHeader("accessToken") @NotEmpty(message = "accessToken is mandatory") String bearer,@RequestHeader("mappingId") @NotEmpty(message = "mappingId is mandatory") String mappingId) {
}

【讨论】:

  • @GetMapping "是" 完全是 @RequestMapping(method = { RequestMethod.GET }) shrtcut
  • 我再次添加了您的代码,现在它显示为 Caused by: java.lang.IllegalStateException: Ambiguous mapping。无法映射“userControllerAPI”方法原因:java.lang.IllegalStateException:不明确的映射。无法映射'userControllerAPI'方法......这个错误可能是我在我认为的类的顶部添加了@requestmapping..
  • 在方法前使用@RequestMapping(method= {RequestMethod.GET}, value = "/getalluserdetails") 并删除@GetMapping 注释。
  • @Mohankumar 你检查过上述解决方案了吗?
  • 是的,但响应相同..它适用于其他方法
猜你喜欢
  • 2014-06-04
  • 1970-01-01
  • 2019-04-11
  • 2018-04-22
  • 2020-11-08
  • 2021-05-09
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多