1. 程式人生 > 其它 >No primary or default constructor found for interface javax.servlet.http.HttpServletRequest

No primary or default constructor found for interface javax.servlet.http.HttpServletRequest

技術標籤:Spring CloudErrorgatewayspring cloud

目錄

錯誤資訊

使用 Spring Cloud GatewayNo 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();
}