Skip to content

Redirect is not followed on 301 with default RestClient #33734

@AntipovAndrey

Description

@AntipovAndrey

Default RestClient does not follow redirect (in case of 301 at least.
Response body is null

@RestController
class SampleController {

    @GetMapping("/first")
    fun redirectToSecond() = ResponseEntity
        .status(301)
        .header("Location", "/second")
        .build<Unit>()

    @GetMapping("/second")
    fun noRedirect() = "OK"
}

@Component
class Runner(
    @Value("\${server.port}")
    private val port: Int
) : ApplicationRunner {

    override fun run(args: ApplicationArguments?) {
        // this works: redirect is followed
        val entityWithRC = RestClient.builder(RestTemplate()) // <---- this
            .baseUrl("http://localhost:$port")
            .build()
            .get()
            .uri("/first")
            .retrieve()
            .toEntity<String>()

        // this does not work: redirect is not followed
        val entityWithDefaults = RestClient.builder()
            .baseUrl("http://localhost:$port")
            .build()
            .get()
            .uri("/first")
            .retrieve()
            .toEntity<String>()

        println(entityWithRC)
        println(entityWithDefaults)
    }
}

Console output:

<200 OK OK,OK,[Content-Type:"text/plain;charset=UTF-8", Content-Length:"2", Date:"Thu, 17 Oct 2024 14:39:00 GMT", Keep-Alive:"timeout=60", Connection:"keep-alive"]>
<301 MOVED_PERMANENTLY Moved Permanently,[content-length:"0", date:"Thu, 17 Oct 2024 14:39:00 GMT", location:"/second"]>

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.comin: webIssues in web modules (web, webmvc, webflux, websocket)status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions