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

Demoonstrate that changing the servlet context path via Spring Boot works. #31

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gregturn
Copy link
Contributor

When applying server.servlet.context-path=/ctx, the links automatically adjust.

2020-04-13 10:43:59.877  INFO 79416 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/ctx'

Testing with curl...

$ curl localhost:8080/ctx/employees | jq
{
  "_embedded": {
    "employees": [
      {
        "id": 1,
        "firstName": "Frodo",
        "lastName": "Baggins",
        "role": "ring bearer",
        "fullName": "Frodo Baggins",
        "_links": {
          "self": {
            "href": "http://localhost:8080/ctx/employees/1"
          },
          "employees": {
            "href": "http://localhost:8080/ctx/employees"
          }
        }
      },
      {
        "id": 2,
        "firstName": "Bilbo",
        "lastName": "Baggins",
        "role": "burglar",
        "fullName": "Bilbo Baggins",
        "_links": {
          "self": {
            "href": "http://localhost:8080/ctx/employees/2"
          },
          "employees": {
            "href": "http://localhost:8080/ctx/employees"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/ctx/employees"
    }
  }
}

When applying `server.servlet.context-path=/ctx`, the links automatically adjust.

```
2020-04-13 10:43:59.877  INFO 79416 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/ctx'
```

Testing with curl...

```
$ curl localhost:8080/ctx/employees | jq
{
  "_embedded": {
    "employees": [
      {
        "id": 1,
        "firstName": "Frodo",
        "lastName": "Baggins",
        "role": "ring bearer",
        "fullName": "Frodo Baggins",
        "_links": {
          "self": {
            "href": "http://localhost:8080/ctx/employees/1"
          },
          "employees": {
            "href": "http://localhost:8080/ctx/employees"
          }
        }
      },
      {
        "id": 2,
        "firstName": "Bilbo",
        "lastName": "Baggins",
        "role": "burglar",
        "fullName": "Bilbo Baggins",
        "_links": {
          "self": {
            "href": "http://localhost:8080/ctx/employees/2"
          },
          "employees": {
            "href": "http://localhost:8080/ctx/employees"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/ctx/employees"
    }
  }
}
```
@avivmu
Copy link
Contributor

avivmu commented Oct 28, 2020

If the issue is closed due to no feedback from issuer / others, should we close / merge this one?

@gcwilliams
Copy link

gcwilliams commented Jan 19, 2021

Hi,

I've notice that this does not work correctly if you are using DeferredResult<T> and generating the HATEOAS link on another thread. It looks like it uses ThreadLocals requestAttributesHolder and inheritableRequestAttributesHolder in

org.springframework.web.context.request.RequestContextHolder#getRequestAttributes

called from

org.springframework.web.servlet.support.ServletUriComponentsBuilder#getCurrentRequest

If those ThreadLocals are not set, it drops the context path in the link that's generated.

I found this issue from spring-projects/spring-hateoas#1268 which has been closed now.

@gregturn gregturn changed the base branch from master to main April 7, 2021 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants