Skip to content

tufangorel/spring-boot-read-http-request-header-write-http-response-header

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-boot-read-http-request-header-write-http-response-header

Purpose : Read http request header sent from client to the server into ThreadLocal as current user context data.
Reason : Return http response header back to the current HttpServletResponse.

Filter, Aspect, Controller and HandlerInterceptor execution order :

                                                            Client HTTP Request ( POSTMAN  )
                                                                        ↓
                    org.springframework.web.filter.OncePerRequestFilter (com.company.customerinfo.filter.PerRequestFilter) doFilterInternal
                                                                        ↓
                                           javax.servlet.Filter (com.company.customerinfo.filter.RequestFilter) doFilter
                                                                        ↓
                                           javax.servlet.Filter (com.company.customerinfo.filter.ResponseFilter) doFilter
                                                                        ↓    
                                    org.aspectj.lang.annotation.Aspect (com.company.customerinfo.aspect.RequestAspect) beforeAdvice
                                                                        ↓
                org.springframework.web.bind.annotation.RestController (com.company.customerinfo.controller) save(@RequestBody Customer customer)
                                                                        ↓
                    org.springframework.web.servlet.HandlerInterceptor (com.company.customerinfo.interceptor) afterCompletion

Local run steps

1- Add "transaction-id" as http request header from POSTMAN request.
2- Start Spring Boot REST API by running main method containing class CustomerInfoApplication.java in your IDE.
3- Alternatively you can start your Docker container by following the commands below.
NOT : Execute maven command from where the pom.xml is located in the project directory to create Spring Boot executable jar.

 
$ mvn clean install -U -X 
$ mvn spring-boot:run

swagger_ui can be accessed via https secure port 8443 from localhost :
https://localhost:8443/customer-info/swagger-ui/index.html

https_swagger_ui

Tech Stack

Java 11
H2 Database Engine
spring boot
spring boot starter data jpa
spring boot starter web
spring boot starter test
spring boot starter aop
spring boot starter actuator
spring security web
springdoc openapi ui
springfox swagger ui
hibernate
logback
maven
hikari connection pool
Docker

Docker build run steps

NOT : Execute docker commands from where the DockerFile is located.
NOT : Tested on Windows 10 with Docker Desktop Engine Version : 20.10.11

$ docker system prune -a --volumes 
$ docker build . --tag demo
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
demo latest 9d4a0ec3294e 6 minutes ago 288MB
$ docker run -p 8443:8443 -e "SPRING_PROFILES_ACTIVE=dev" demo:latest

API OPERATIONS

Save a new customer to database

Method : HTTP.POST
URL : https://localhost:8443/customer-info/customer/save
HTTP Request Body :

{
    "name": "name1",
    "age": 1,
    "shippingAddress": {
        "address": {
            "streetName": "software",
            "city": "ankara",
            "country": "TR"
        }
    }
}

HTTP Request Headers :

transaction-id: 123-123-123
Content-Type: application/json
User-Agent: PostmanRuntime/7.28.4
Accept: */*
Postman-Token: aee7b189-8004-4fd7-a050-bd4e09bba6bd
Host: localhost:8443
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 206

Curl Request :

curl --location --request POST 'https://localhost:8443/customer-info/customer/save' \
--header 'transaction-id: 123-123-123' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "name1",
    "age": 1,
    "shippingAddress": {
        "address": {
            "streetName": "software",
            "city": "ankara",
            "country": "TR"
        }
    }
}'

Response :

HTTP response code 200

{
    "id": 1,
    "name": "name1",
    "age": 1,
    "shippingAddress": {
        "id": 1,
        "address": {
            "id": 1,
            "streetName": "software",
            "city": "ankara",
            "country": "TR"
        }
    }
}

HTTP Response Headers :

request-id: 68182bbf-996d-4732-a6ff-2c49a90012d1
correlation-id: 68182bbf-996d-4732-a6ff-2c49a90012d1
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers

List all customers saved to database

Method : HTTP.GET
URL : https://localhost:8443/customer-info/customer/list
Request Body :

{}

Curl Request :

curl --location --request GET 'https://localhost:8443/customer-info/customer/list' \
--header 'Content-Type: application/json' \
--header 'Cookie: JSESSIONID=5E6B21C9533643F4A7EE462DCBB3B312' \
--data-raw '{}'

Response :

HTTP response code 200

[
    {
        "id": 1,
        "name": "name1",
        "age": 1,
        "shippingAddress": {
            "id": 1,
            "address": {
                "id": 1,
                "streetName": "software",
                "city": "ankara",
                "country": "TR"
            }
        }
    }
]

HTTP Response Headers :
request-id: 411b4b33-6af5-4f78-b185-4171e779222d
correlation-id: 411b4b33-6af5-4f78-b185-4171e779222d
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers

About

spring-boot-read-http-request-header-write-http-response-header

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published