5

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.

user2914191
  • 877
  • 1
  • 8
  • 21

1 Answers1

8

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.

benjamin c
  • 2,278
  • 15
  • 25
  • bingo. specifying `ServerHttpRequest request` in my method correctly injects `org.springframework.http.server.reactive.ServerHttpRequest`. – user2914191 Sep 04 '18 at 15:23
  • for anyone who needs more details, obtain IP address with `request.getRemoteAddress().getAddress().getHostAddress()` – user2914191 Sep 04 '18 at 15:30