Spring Boot无法识别具有发送请求参数的控制器

时间:2019-10-05 14:33:32

标签: java spring spring-boot

仅当我根据要求发送更多参数时,弹簧靴才会识别我的控制器。例如:

如果我发送正常的 GET ,请请求弹簧启动识别我的控制器: http://localhost/idp/oauth/123/authorize

如果我使用扩展参数发送 GET 请求,则弹簧靴无法识别我的控制器: http://localhost/idp/oauth/123/authorize?scope=public_profile

我需要完全接收第二个示例的请求(具有参数范围),但是spring boot无法识别控制器,并重定向到/ 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) {
            // ...
    }
}

2 个答案:

答案 0 :(得分:2)

由于您要传递名称为“ 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) {
        // ...
}

答案 1 :(得分:1)

您错过了控制器方法定义中的@RequestParam

More on @RequestParam