java - Spring Boot 无法识别带有发送请求参数的 Controller

标签 java spring spring-boot

仅当我根据请求发送更多参数时,Spring Boot 才无法识别我的 Controller 。例如:

如果我发送正常的GET请求,Spring Boot识别我的 Controller : http://localhost/idp/oauth/123/authorize

如果我发送带有额外参数GET请求,Spring Boot无法识别我的 Controller : http://localhost/idp/oauth/123/authorize?scope=public_profile

我需要准确接收第二个示例的请求(带有参数范围),但 Spring Boot 无法识别 Controller 并重定向到/error。

代码:

@Controller
@RequestMapping("/idp/oauth")
public class OAuthController {

    @RequestMapping(value = "/{clientId}/authorize", method = RequestMethod.GET)
    public String authorizeGet(
            HttpServletRequest request, 
            HttpServletResponse response, 
            @PathVariable String clientId,
            Model model) {
            // ...
    }

    @RequestMapping(value = "/{clientId}/authorize", method = RequestMethod.POST)
    public String authorizePost(
            HttpServletRequest request, 
            HttpServletResponse response, 
            @PathVariable String clientId,
            Model model) {
            // ...
    }
}

最佳答案

由于您传递了名为“scope”的额外参数,Spring 将在方法中搜索 @RequestParam 它找不到任何内容,因此出现错误

您需要修改您的方法以添加所有@RequestParam

如果可选字段不是必需的,您还可以添加 required = false

@RequestMapping(value = "/{clientId}/authorize", method = RequestMethod.GET)
public String authorizeGet(
        HttpServletRequest request, 
        HttpServletResponse response, 
        @PathVariable String clientId,
        @RequestParam(value = "scope") String scope,
        @RequestParam(required = false, value = "optionalParam") String optionalParam,
        Model model) {
        // ...
}

关于java - Spring Boot 无法识别带有发送请求参数的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58249280/

相关文章:

java - 这个java后增量运算符在做什么?

java - 传递参数时进行计算

java - Spring 数据 neo4j LIKE 查询不起作用

java - 获取 org.dom4j.DocumentException : feature read only while deploying war file in tomcat of linux 异常

java - Spring Autowiring,我只有一个实现应该 Autowiring 吗?如果是这样,我应该 Autowiring 实现吗?

java - Maven 和 "is not a known entity type"错误

java - Hibernate:将父实体映射到子实体,并有 2 个对子实体中一列的引用?

java - 如何将计划任务与还使用 keycloak 提供网页的客户端一起使用?

java - 由于多个可能的 bean,Spring Boot Autowiring 泛型失败

MySQL/JPA 字段没有默认值错误,即使该字段位于类层次结构中的不同类中