Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing slash added for request mappings with empty path #1886

Closed
Patouche opened this issue Jan 5, 2023 · 0 comments
Closed

Trailing slash added for request mappings with empty path #1886

Patouche opened this issue Jan 5, 2023 · 0 comments
Assignees
Labels
in: core Core parts of the project stack: webmvc type: bug
Milestone

Comments

@Patouche
Copy link
Contributor

Patouche commented Jan 5, 2023

Hello !

This may be related to this issues spring-projects/spring-framework#28552 or spring-projects/spring-framework#29625 but there is a strange behavior using spring hateoas with the removal of the trailing slash.

For example:

package com.sample

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.hateoas.CollectionModel
import org.springframework.hateoas.RepresentationModel
import org.springframework.hateoas.server.mvc.add
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

data class User(val id: Int) : RepresentationModel<User>()

@SpringBootApplication
@RestController
@RequestMapping("/users")
class SampleApp {
    @GetMapping("")
    fun list() = CollectionModel.of((1..10).map { User(it) })

    @GetMapping("/{id}")
    fun single(@PathVariable id: Int) = ResponseEntity.ok(User(id).add(SampleApp::class) { linkTo { list() } withRel "up" })

}

fun main(args: Array<String>) {
    runApplication<SampleApp>(*args)
}

The endpoint http://localhost:8080/users/1 will produce the following output:

{
  "id":1,
  "_links":{
    "up": {
      "href":"http://localhost:8080/users/"
    }
  }
}

As you can notice the up link isn't correct as it will lead to 404 error page with the removal of the trailing slash. If we transform the @GetMapping("") into a @GetMapping, the link will become correct.

For the time being, I don't have time to find the root cause of this issue but I can try to take a look of what's happens here.

Regards,
Patrick

@odrotbohm odrotbohm changed the title Spring hateos generate links leading to 404 error Trailing slash added for request mappings with empty path Jan 12, 2023
@odrotbohm odrotbohm self-assigned this Jan 12, 2023
@odrotbohm odrotbohm added type: bug stack: webmvc in: core Core parts of the project labels Jan 12, 2023
@odrotbohm odrotbohm added this to the 2.0.1 milestone Jan 12, 2023
@odrotbohm odrotbohm modified the milestones: 2.0.1, 2.1.0-M1 Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Core parts of the project stack: webmvc type: bug
Projects
None yet
Development

No branches or pull requests

2 participants