spring boot (webflux) rest controller get remote IP address

user2914191 :

Using spring boot for simple REST application.

<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>

I have a simple controller that handles requests.

@RestController
@RequestMapping("/api")
public class MainController {

    @RequestMapping("/test")
    public BasicDTO getBasic(HttpServletRequest request){
        System.out.println(request.getRemoteAddr());
        return new BasicDTO();
    }

}

The HttpServletRequest context does not get injected. How can I inject the request context into the method so that I can access some basic socket details? Thanks.

benjamin c :

Looking at your pom.xml it uses spring-boot-starter-webflux so you must use ServerHttpRequest instead of HttpServletRequest.

Or include,

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

and remove spring-boot-starter-webflux dependency.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=93087&siteId=1