简体   繁体   中英

How to implement correlation id in Spring Boot REST API

I want to implement correlation id in Spring Boot and want to send that in the response body, can anyone please help me on this. I am new a budding Java developer.

{ "correlationGuid": "a33b177f-9ea1-4d5d-9f23-b6e4a4a627b5" // Then the main content of response. }

The direct answer to your question is simple, however it's all about what you do with this generated UUID . Afaik the correlation id's purpose is to keep track of requests/responses, so you'd have to log them somewhere to be useful.

But sending an UUID back is as simple as:

class MyResponse {

    private UUID id;
    // your fields

    public MyResponse() {
        this.id = UUID.randomUUID();
        // your constructor
    }

    // your methods

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM