错误信息
使用 Spring Cloud Gateway 报 No primary or default constructor found for interface javax.servlet.http.HttpServletRequest 错误。另有人说换成 org.springframework.http.server.ServerHttpRequest 还是报错。

错误详细信息如下:
java.lang.IllegalStateException: No primary or default constructor found for interface javax.servlet.http.HttpServletRequest
at org.springframework.web.reactive.result.method.annotation.ModelAttributeMethodArgumentResolver.createAttribute(ModelAttributeMethodArgumentResolver.java:210) ~[spring-webflux-5.2.1.RELEASE.jar:5.2.1.RELEASE]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ org.springframework.web.cors.reactive.CorsWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
|_ checkpoint ⇢ HTTP GET "/sign/sis/home" [ExceptionHandlingWebHandler]
问题定位
在 Spring Cloud Gateway 编写的 Controller 接口方法,不能用 javax.servlet.http.HttpServletRequest,因为Spring Cloud Gateway 不支持 HttpServletRequest 。
@Slf4j
@RestController
@RequestMapping("/sign")
public class SignController {
@GetMapping(value = "/sis/home")
public JsonResult toSisHome(HttpServletRequest request) {
return JsonResult.success();
}
}
解决
把 javax.servlet.http.HttpServletRequest 换成 org.springframework.http.server.reactive.ServerHttpRequest 即可。
@GetMapping(value = "/sis/home")
public JsonResult toSisHome(ServerHttpRequest request) {
return JsonResult.success();
}

在SpringCloudGateway中遇到使用javax.servlet.http.HttpServletRequest报错的问题,错误信息为'Noprimaryordefaultconstructorfoundforinterfacejavax.servlet.http.HttpServletRequest'。问题在于SpringCloudGateway不支持HttpServletRequest。解决方案是将HttpServletRequest替换为org.springframework.http.server.reactive.ServerHttpRequest。更新后的代码示例展示了在Controller接口方法中如何正确使用ServerHttpRequest。
1466





