spring boot (webflux) rest 控制器获取远程 IP 地址
spring boot (webflux) rest controller get remote IP address
为简单的 REST 应用程序使用 spring 引导。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
我有一个处理请求的简单控制器。
@RestController
@RequestMapping("/api")
public class MainController {
@RequestMapping("/test")
public BasicDTO getBasic(HttpServletRequest request){
System.out.println(request.getRemoteAddr());
return new BasicDTO();
}
}
HttpServletRequest
上下文没有被注入。如何将请求上下文注入方法,以便我可以访问一些基本的套接字详细信息?谢谢
查看您的 pom.xml
它使用 spring-boot-starter-webflux
因此您必须使用 ServerHttpRequest
而不是 HttpServletRequest
。
或包括,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
并删除 spring-boot-starter-webflux
依赖项。
为简单的 REST 应用程序使用 spring 引导。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
我有一个处理请求的简单控制器。
@RestController
@RequestMapping("/api")
public class MainController {
@RequestMapping("/test")
public BasicDTO getBasic(HttpServletRequest request){
System.out.println(request.getRemoteAddr());
return new BasicDTO();
}
}
HttpServletRequest
上下文没有被注入。如何将请求上下文注入方法,以便我可以访问一些基本的套接字详细信息?谢谢
查看您的 pom.xml
它使用 spring-boot-starter-webflux
因此您必须使用 ServerHttpRequest
而不是 HttpServletRequest
。
或包括,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
并删除 spring-boot-starter-webflux
依赖项。